Looking at the following class definition:
class MyClass
{
public:
int value;
std::vector<int> container{value, value}; //vector initialized using member
MyClass(size_t v) : value(v){} //initialize value in initializer list
};
int main(){
MyClass obj(10);
for(auto& i : obj.container){ //this works as expected
std::cout << i << " ";
}
}
The member value
is initialized in the initializer list, and it's used in the initializer list constuctor of a std::vector
member.
The initialization works as expected, std::vector
is initialized with two member whose value is that of value
member.
Of course I could simply do this:
MyClass(size_t v) : value(v), container{value, value}{}
But that's not the point.
I'd assume there are rules that govern this situation but I've not been able to find them.
The question is, is that construct correct? How can I be sure value
is initialized before the vector?
Aucun commentaire:
Enregistrer un commentaire