0

Suppose a class D maintaining a vector of objects of class C, and suppose that the first three elements of the vector shall be accessible through data members field0 .. field2, which are references to objects of class C.

If we initialise data members field0 .. field2 at the point of their declaration, and none of the constructors of D uses explicit mem-initializer lists, is it guaranteed that the data members will always be initialised with a correct reference to the objects held in the vector (if we consider subclassing, copying, moving, ... an object of class D)?

Note that the question is not about the order of initialization but more on "can anything in the use of class D (including subclassing) prevent the data members from being initialised"?

class C{
};

class D{
protected:
    std::vector<C> myC = std::vector<C>(3);
public:
    C &field0 = myC[0];
    C &field1 = myC[1];
    C &field2 = myC[2];

    D () {};  // no mem-initializers
};
Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
  • Actually it's not about the order of initialization but more on "can anything in the use of class `D` (including subclassing) prevent the data members from being initialised? – Stephan Lechner May 29 '17 at 09:53
  • I'm not sure about initialization, but I'd gladly brick your implementation by resizing the vector in a subclass until it allocates a new memory area. – grek40 May 29 '17 at 12:35

0 Answers0