Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

derived class constructor calling base class constructor?

Posted on 2007-04-02
8
Medium Priority
?
432 Views
Last Modified: 2008-02-01
Hi,

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?

Thanks
0
Comment
Question by:DJ_AM_Juicebox
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 18841224
>> 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.
0
 
LVL 86

Expert Comment

by:jkr
ID: 18841228
>>for a good reason

Message maps etc.
0
 
LVL 86

Expert Comment

by:jkr
ID: 18841233
Um, also take a look at the expansion of these macros, which will shed in somoe more light.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 11

Expert Comment

by:Deepu Abraham
ID: 18841293
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,
DeepuAbrahamK
0
 
LVL 30

Expert Comment

by:Axter
ID: 18841308
>>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.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 18841585
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.

   base              
                     derived
 [[                        ]                      ]
 
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
0
 
LVL 1

Expert Comment

by:ruizpauker
ID: 18895405
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:

public:
  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 {
  public:
    A() { cout << "A constructor";  }
};

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

class C : A, B {
  public:
    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
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question