0

While scrolling through SO, I found this question

#include <iostream>

using namespace std;

class base
{
   public:
      base()
      {
         cout << "ctor in base class\n";
      }
};

class derived1 : public base
{
   public:
      derived1()
      {
         cout <<"ctor in derived class\n";
      }
};

int main()
{
   derived1 d1obj;
   return 0;
}

There was this question :- When d1obj is created, the control first reaches the base class constructor and then it goes to the derived class constructor? Or is it the other way round : It first reaches the derived class constructor, finds that it has base class and so the control goes to the constructor in base class?

and the aswer contained the part

When a class has virtual bases it's common for a constructor to result in two different function bodies being emitted - one for use when this class is the most derived type, and one for use when this class is itself a base class. The reason is that virtual base classes are constructed by the most-derived class, to ensure that when they're shared they're only constructed once. So the first version of the constructor will call all base class constructors, whereas the second version will only call constructors for non-virtual bases.

can anyone explain to me this with an example ?

Here is the link for that question

Community
  • 1
  • 1
Lion's_Den
  • 121
  • 1
  • 7
  • 1
    Your example doesn't have a virtual base-class, so can't be used to explain the bold italic part of your question. It is a special case of how the compiler constructs the constructor specifically for virtual inheritance ("diamond inheritance") where two classes your class derives from has the same base-class. I've never had to use that in my 10+ years of programming C++ (other than "here's an example of virtual inheritance") – Mats Petersson Apr 05 '17 at 06:35

2 Answers2

1

In your example it is the default constructor of derived1 that is responsible for calling the default constructor of base. However, this is only necessary when derived1 is the actual (most derived) object, as in your example

int main()
{
  derived1 d1obj; // Most derived object of type `derived1`
  // It contains a direct `base` subobject
  // Constructor of `derived1` must construct `base` subobject as well
} 

But when derived1 is used a base subobject of a larger object

class derived2 : public derived1
{
public:
  derived2()
  {
     cout <<"ctor in derived 2 class\n";
  }    
};

int main()
{
  derived2 d2obj; // Most derived object of type `derived2`
  // It contains a direct `derived1` subobject and an indirect `base` subobject 
  // (through `derived1`)
  // Constructor of `derived2` must directly construct both `derived1` subobject and 
  // `base` subobject 
  // Which means that constructor of `derived1` subobject should not attempt
  // to construct `base` subobject
}

then it is the constructor of derived2 that is responsible for calling both the default constructor of derived1 and the default constructor base. In this case the default constructor of derived1 shall not call the constructor of base, since it will lead to double-construction of base.

To satisfy all of these requirements derived1 will typically have two versions of its default constructor: full version called in your original example and "reduced" version called from derived2 in my example above.

Alternatively, derived1's default constructor can be implemented with a hidden boolean parameter, which will tell it whether to call base's default construictor or not.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
0

"When a class has virtual bases it's common for a constructor to result in two different function bodies being emitted - one for use when this class is the most derived type, and one for use when this class is itself a base class. The reason is that virtual base classes are constructed by the most-derived class, to ensure that when they're shared they're only constructed once. So the first version of the constructor will call all base class constructors, whereas the second version will only call constructors for non-virtual bases."

in this the answerer actually wants to say about a situation common in C++ known as dreaded diamond in multiple inheritance. to understand just see the most upvoted answer to this link In C++, what is a virtual base class?

Community
  • 1
  • 1
Anshul
  • 76
  • 9