Constructor call during inheritance

A

appu

Hi all,
Suppose class A is inherited from class B and Class B is inherited
from class C. Could anyone explain on how the C++ compiler construct
the A object and how is A object destroyed by compiler when it is out
of scope. Can anyone explain what does the compiler do in these
process?

thanks in advance

-karups
 
A

Alf P. Steinbach

* appu:
Suppose class A is inherited from class B and Class B is inherited
from class C. Could anyone explain on how the C++ compiler construct
the A object and how is A object destroyed by compiler when it is out
of scope. Can anyone explain what does the compiler do in these
process?

This seems like a HOMEWORK question rephrased for clc++.

Please read the FAQ items about how to post, and about HOMEWORK questions.

That said, what the compiler does is irrelevant to the question of
construction and destruction. In particular, the compiler does not
construct objects, as you seem to think it does. The compiler
translates your C++ source code to executable form.

Only what happens in the program is relevant with respect to object
construction and destruction.

And one easy way you can check up on that (I'm not going to give you the
conclusions, since this is most likely HOMEWORK) is to embed output
statements in the constructors and destructors for classes A, B and C.

Although that will not tell you the full story, it will provide a good
starting point for understanding things.

What's left to understand when you have done the above (just ask here if
you run into difficulties): the order of execution for initializer list
expressions, what happens with virtual inheritance, what happens when a
constructor throws an exception, what happens with initialization of
arrays, ditto when a constructor for an array element throws, what
happens with virtual calls from constructors, under what conditions &
when can *this be used during construction, & more... Enjoy your
research into these matters. And again, just ask if you need, but then,
please post the C++ CODE (copy and paste), what your expectations were,
why you expected that, and what actually happened that was unexpected.

Cheers, & hth.,

- Alf
 
J

James Kanze

Suppose class A is inherited from class B and Class B is inherited
from class C. Could anyone explain on how the C++ compiler construct
the A object and how is A object destroyed by compiler when it is out
of scope. Can anyone explain what does the compiler do in these
process?

The rule is that the compiler generates calls to the base class
constructors (using the initialization arguments if there are
any, and the default constructor otherwise) and all member
objects before entering into its own body. Thus, in this case,
the compiler will generate a call to the constructor of B at the
top of the constructor of A. (The same thing happens with B, of
course, so the constructor of B calls the constructor of C.)

Destruction works the same way, except that the calls to the
base class destructors are at the end of the derived class
constructor.

If this isn't described in your basic C++ text, it's time to get
a different text.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top