Home » Coding » c++ » Constructor call during inheritance
Constructor call during inheritance [message #17346] Sat, 06 October 2007 08:37 Go to next message
appu  is currently offline appu
Messages: 1
Registered: October 2007
Junior Member
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
Re: Constructor call during inheritance [message #17347 is a reply to message #17346 ] Sat, 06 October 2007 09:14 Go to previous messageGo to next message
alfps  is currently offline alfps
Messages: 315
Registered: July 2007
Senior Member
* 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

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Re: Constructor call during inheritance [message #17383 is a reply to message #17346 ] Sun, 07 October 2007 05:32 Go to previous message
James Kanze  is currently offline James Kanze
Messages: 598
Registered: July 2007
Senior Member
On Oct 6, 2:37 pm, appu <karuppaiah...@gmail.com> wrote:

> 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.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Previous Topic:Optimization: const int& ?
Next Topic:Re: Reverse a String
Goto Forum:
  


Current Time: Fri May 16 02:41:33 EDT 2008

Total time taken to generate the page: 0.77886 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 2.7.7.
Copyright ©2001-2007 FUD Forum Bulletin Board Software