How to initialize member const-vector of C ++ class

I have a constant vector as a member in a class. How to initialize it?

I know that for constant members, the only way to initialize them is to initialize the list in the class constructor function. But I don’t know exactly how to do this with the vector.

For instance,

class ClassA { const std::vector<int> m_Vec; }; 

thanks

+5
source share
4 answers

In C ++ 03, you initialize the vector through the constructor, which is rather inconvenient, since you have to copy it from some other place. For instance:

 static int initializer[] = {1, 2, 3}; ClassA::ClassA() : m_Vec(initializer, initializer + sizeof(initialize) / sizeof(int)) {} 

In C ++ 11, all this disappears. In the constructor:

 ClassA::ClassA() : m_Vec{1, 2, 3, 4} {} 

In place

 std::vector<int> m_Vec{1, 2, 3, 4}; 
+7
source

You can initialize a vector in a class declaration

 class ClassA { const std::vector<int> m_Vec = {1,2,3}; }; 

Or in the constructor using member initialization

 class ClassA { public: ClassA(std::vector<int> const& vec) : m_Vec(vec) {} private: const std::vector<int> m_Vec; }; 
+7
source

I have a constant vector as a member in a class.

First, I would say that you want to avoid this in order to prevent the class from being used in the expected ways (see example below). In addition, if a data element is fully encapsulated in a class, there is no reason why it cannot be a non-constant data element when using the class interface to ensure that it is processed in const (i.e. class instances do not have the ability to modify the data element) .

Bad example

 class ClassA { const std::vector<int> m_Vec; }; int main() { ClassA c1; ClassA c2; // ... c1 = c2; // This doesn't compile! return 0; } 

How can I initialize it?

Other answers provide several different ways to initialize a const data element, but there is one that I have not seen in other answers : use a function . I think that the advantage of using a function can be if the initialization is complicated and possibly depends on the input at runtime (for example, you can update the init function to take an external state to change the way std::vector initialized).

Free function example

 #include <vector> namespace { std::vector<int> init() { std::vector<int> result; // ... do initialization stuff ... return result; } } class ClassA { public: ClassA() : m_Vec(init()) {} private: const std::vector<int> m_Vec; }; int main() { ClassA c1; return 0; } 

Member Function Example

 #include <vector> class ClassA { public: ClassA() : m_Vec(init()) {} private: std::vector<int> init() { std::vector<int> result; // ... do initialization stuff ... return result; } const std::vector<int> m_Vec; }; int main() { ClassA c1; return 0; } 
+6
source

Like this:

 class ClassA { const std::vector<int> m_Vec {1,2,3}; }; 

Or that:

 class ClassA { ClassA () : m_Vec {1,2,3} {} const std::vector<int> m_Vec; }; 
+3
source

Source: https://habr.com/ru/post/1243883/


All Articles