derived class constructor calling base class constructor?


I have a CView derived class A, then another class derived from A, called class B. When I create it in a splitter window:

    CreateView(0, 0, RUNTIME_CLASS(CMyClassB)

somehow the constructor of base class A gets called first, then the constructor of class b!!

I know this is MFC and not pure C++, but I thought the constructor of a base class is never called when a derived class is being created? Or is that wrong?

Who is Participating?
jkrConnect With a Mentor Commented:
>> Or is that wrong?

No, but MFC code usually calls the base constructors explicitly (for a good reason, regarding how MFC is organzied) via either IMPEMENT_DYNCREATE() or other framework helpers.
>>for a good reason

Message maps etc.
Um, also take a look at the expansion of these macros, which will shed in somoe more light.
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Deepu AbrahamR & D Engineering ManagerCommented:
Typically, while creating an object of a derived class we call the base class constructor as well because there are a lot of ground work done in base class rather than derived.Derived class is just the specialisation of the base.If base is not there then there is no sense for the Derived class.

When you call the base class contructor explicitly, that means you are instructing the compiler which constructor you want to call (Mostly in case of overloaded ones)

Best Regards,
>>but I thought the constructor of a base class is never called when a derived class is being created? Or is that wrong?

Regardless if it's C++ or MFC, the base class constructor always gets called before the derive class constructor.  That's a core rule of the C++ language.

When the object is destroyed, then you get the opposite effect, in that the derive class destructor gets called before the base class destructor.
Axter is right. You should think of a baseclass as being enhanced by a derived class. In case of single inheritance the baseclass pointer and the derived pointer usually are pointing to the same address.

 [[                        ]                      ]
In the constructor of the derived class you can access baseclass members but not viceversa. For the same reasons yo can't call virtual functions In the baseclass constructor cause the deriveed classes were not fully constructed yet.

Regards, Alex
In C++ when you derive from a class,
class A { ... };
class B : A { ... };
it could be seen as if B contains an instance of A, just as itsmeandnobodyelse said, now, there exists the need of A for the existence of B, so, A must be enterely created and instanced for B to be instanced. Hence, if you have especialized constructors in A, you could specify in B which constructor of A should be used, this way:

  B() : A(3)
     // calling the A constructor's that receives an int

Notice also that, when you don't write a default constructor for a class, the compiler provides one, so there always exists at least one constructor in a class, the default constructor.

Give a try to this:

class A {
    A() { cout << "A constructor";  }

class B {
    B() { cout << "B constructor";  }

class C : A, B {
    C() { cout << "C constructor";  }

void main()
  C c;

here we have multiple inheritance, C derives from A and B, here and instance of C will be compound of one instance of A and one instance of B, now, C will only be instanced when A and B have been instanced, not before.

So, run the code, and you will see that constructors of A and B are executed before C constructor, but, don't confuse with constructor calling. If you debug this code, you will see that when you create an instance of C, it will first call C constructor's, but it will not be executed untill C is complete created, all its variables and all the base objects have been created, (see as if A and B are the foundation of C), now, for A and B to be created the compiler needs to call its constructors, here, the compiler calls A constructor, and as A has no base class it's completed, and the A constructor's is executed, then the control returns to C constructor's call, but, C is not already complete, so it calls the B constructor's, and again, as B has no base class it gets executed, and returns the control to C constructor's, that now is complete and executes it.

As a final comment, if A or B were derived from another class, it should be created first, and its base class constructor should be called and executed before this class constructor.

If you want me to continue with this explanation about hierarchy and take an deeper tour of objects hierarchy ask me to doit, i will be pleased to explain you about some troubles with static inheritance and talk a little about virtual inheritance.

Hopping to solve your question,

Paul Ruiz Pauker
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.