1

Consider

 class A { }; class B { B(A * in); }; class C : public A { B b; public: C():b(this){} }; 

Is the constructor in C safe? Is member of A already available (and constructed) ?

1
  • Define "safe".. Commented Sep 6, 2016 at 22:42

1 Answer 1

4

yes. All Base classes are constructed before the rest of the constructor is executed.

For example, from the Stroustrup C++ 2011 book:

17.2.3 Base and Member Destructors Constructors and destructors interact correctly with class hierarchies (§3.2.4, Chapter 20). A constructor builds a class object ‘‘from the bottom up’’: [1] first, the constructor invokes its base class constructors, [2] then, it invokes the member constructors, and [3] finally, it executes its own body. A destructor ‘‘tears down’’ an object in the reverse order: [1] first, the destructor executes its own body, [2] then, it invokes its member destructors, and [3] finally, it inv okes its base class destructors. 
Sign up to request clarification or add additional context in comments.

1 Comment

Except for when B's constructor tries to invoke a virtual function on in.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.