Search code examples
c++vectorconstructorprogram-entry-point

Create a vector as a function call parameter


I have no idea what i have to do in this situation..

Class Build_Using_Vector : public Another
{
 private:
         int Thing;
         vector<int> List_Elem;
 public:
         Build_Using_Vector(int TThing, vector<int> LList_Elem);
};

ok this is not a prob... the second part still is not a prob:

Build_Using_Vector::Build_Using_Vector(int TThing, vector<int> LList_Elem) : Another() 
 {
    Thing = TThing;
    List_Elem = LList_Elem;
 }

As i said this is not a prob.

The prob is there, in "main". In my case is not "main" is another class... but i think is the same...

int main()
 {
   Build_Using_Vector Foo(5, ???) // i don't know how to build with a multiple of element for example 5,6,7)
   return 0;
 }

thank you


Solution

  • You can use list initilization. Build_Using_Vector Foo(5, ???) would be

    Build_Using_Vector Foo(5, {5,6,7})
    

    I would also suggest you use all member initialization with your constructor and change

    Build_Using_Vector::Build_Using_Vector(int TThing, vector<int> LList_Elem) : Another() 
    {
        Thing = TThing;
        List_Elem = LList_Elem;
    }
    

    To

    Build_Using_Vector::Build_Using_Vector(int TThing, vector<int> LList_Elem) : Another() , Thing(TThing), List_Elem(LList_Elem) {}