Constructor call during inheritance

Discussion in 'C++' started by appu, Oct 6, 2007.

  1. appu

    appu Guest

    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
     
    appu, Oct 6, 2007
    #1
    1. Advertising

  2. * 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?
     
    Alf P. Steinbach, Oct 6, 2007
    #2
    1. Advertising

  3. appu

    James Kanze Guest

    On Oct 6, 2:37 pm, appu <> 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:
    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
     
    James Kanze, Oct 7, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt Graham
    Replies:
    3
    Views:
    515
    Matt Graham
    Feb 7, 2004
  2. lallous

    Constructor call constructor

    lallous, Mar 5, 2004, in forum: C++
    Replies:
    3
    Views:
    36,945
    Chris \( Val \)
    Mar 5, 2004
  3. Replies:
    10
    Views:
    615
    msalters
    May 9, 2005
  4. Replies:
    9
    Views:
    964
    Alf P. Steinbach
    Mar 6, 2006
  5. Generic Usenet Account
    Replies:
    10
    Views:
    2,243
Loading...

Share This Page