Solved

derived class constructor calling base class constructor?

Posted on 2007-04-02
8
417 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
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
maze travler 6 47
C Language combined operators 28 101
IdTelnet1DataAvailable handler freezes the Application 4 49
c++ syntax question 9 34
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

706 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now