How to initialize a member variable of a vector in a class definition (C ++ 11)
What you do is right. It initializes the data member to have the values 1, 2, 3 whenever you create an instance of VectorInit .
However, if you need another constructor that requires () , you need an alternative syntax:
std::vector<int> m_vector = std::vector(5, 42);
Which compiler is right?
The syntax is valid C ++, so non-VS compilers are right.
If your compiler does not support this type of initialization, you can add a constructor to your class as a workflow:
class VectorInit { public: VectorInt() : m_vector{1, 2, 3}{} private: std::vector<int> m_vector; };
source share