derived class constructor calling base class constructor?

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
DJ_AM_JuiceboxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
>> 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jkrCommented:
>>for a good reason

Message maps etc.
0
jkrCommented:
Um, also take a look at the expansion of these macros, which will shed in somoe more light.
0
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

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,
DeepuAbrahamK
0
AxterCommented:
>>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
itsmeandnobodyelseCommented:
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
ruizpaukerCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.