Solved

derived class constructor calling base class constructor?

Posted on 2007-04-02
8
424 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 500 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
Technology Partners: 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!

 
LVL 11

Expert Comment

by:DeepuAbrahamK
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

Technology Partners: 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

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

752 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