Initialize array or vector over constructor's initializing list

15,944

Solution 1

With C++11, you do it like this:

class MyClass{
           private:
           std::string commands[2];
           //std::vector<std::string> commands;

           public:
           MyClass( std::string command1, std::string command2)
             : commands{command1,command2}
           {/* */}
};

For pre-C++11 compilers, you will need to initialize the array or vector in the body of the constructor:

class MyClass{
           private:
           std::string commands[2];

           public:
           MyClass( std::string command1, std::string command2)
           {
               commands[0] = command1;
               commands[1] = command2;
           }
};

or

class MyClass{
           private:
           std::vector<std::string> commands;

           public:
           MyClass( std::string command1, std::string command2)
           {
               commands.reserve(2);
               commands.push_back(command1);
               commands.push_back(command2);
           }
};

Solution 2

class Myclass{
private:
    Vector<string> *commands;
    // std::vector<std::string> commands(2); respectively 

    public:
    MyClass( std::string command1, std::string command2)
    {
        commands.append(command1);  //Add your values here
    }
}

Solution 3

In the initializer list you may call any constructor of the class of the member you want to initialize. Take a look at std::string and std::vector documentation and choose the constructor that works for you.

For storing two objects I would recommend using std::pair. However if you expect that the number may grow std::vector is the best option.

Solution 4

You could use

#include <utility>

...
std::pair<string, string> commands;
commands=std::make_pair("string1","string2");

...
//to access them use
std::cout<<commands.first<<" "<<commands.second;
Share:
15,944

Related videos on Youtube

fotinsky
Author by

fotinsky

Hi. My name is Fotios and I am a research associate in the field of embedded systems.

Updated on September 14, 2022

Comments

  • fotinsky
    fotinsky over 1 year

    How can I initialize a (string) array or vector with a constructor's initializing list in C++?

    Consider please this example where I want to initialize a string array with arguments given to the constructor:

    #include <string>
    #include <vector>
    
    class Myclass{
               private:
               std::string commands[2];
               // std::vector<std::string> commands(2); respectively 
    
               public:
               MyClass( std::string command1, std::string command2) : commands( ??? )
               {/* */}
    }
    
    int main(){
              MyClass myclass("foo", "bar");
              return 0;
    }
    

    Besides that, which of the two types (array vs vector) is recommended for saving two strings while creating an object, and why?

    • chris
      chris over 10 years
      Use braces, just like you normally would.