Sequence of constructor calls and destructor calls.

Discussion in 'C++' started by Honne Gowda A, Oct 31, 2003.

  1. Hi,
    Can anybody clarify why destruction happens in reverse way?
    for example A<-B<-C, when C's object is constructed, ctor calls will A(),B() and
    C(), when this object is destroyed, sequence of calls will be ~C(),~B(),and
    ~C(). I am wondering why cant' we destroy the object like ctor way?..Are there
    any good reasons for this?

    thanks,
    -Honne
    --
     
    Honne Gowda A, Oct 31, 2003
    #1
    1. Advertising

  2. "Honne Gowda A" <> wrote in message
    news:...
    > Hi,
    > Can anybody clarify why destruction happens in reverse way?
    > for example A<-B<-C, when C's object is constructed, ctor calls will A(),B()

    and
    > C(), when this object is destroyed, sequence of calls will be ~C(),~B(),and
    > ~C(). I am wondering why cant' we destroy the object like ctor way?..Are there
    > any good reasons for this?


    Logically if you agree that before becoming a C object it first becomes an A,
    then B and *then* a C object.
    (Constructor call sequence)
    Then you should also agree that when it has to destruct it should first become B
    from C, and then A from B and then nothing!

    If I were to make a pile of books I would build it one over another. But when I
    were to remove it I would go the other way from top to bottom, unless I want the
    whole pile to fall down :)

    HTH,
    J.Schafer
     
    Josephine Schafer, Oct 31, 2003
    #2
    1. Advertising

  3. Honne Gowda A wrote:
    >
    > Hi,
    > Can anybody clarify why destruction happens in reverse way?
    > for example A<-B<-C, when C's object is constructed, ctor calls will A(),B() and
    > C(), when this object is destroyed, sequence of calls will be ~C(),~B(),and
    > ~C().


    You mean ~A() :)

    > I am wondering why cant' we destroy the object like ctor way?..Are there
    > any good reasons for this?
    >


    Generally it is most always a good idea to undo things in the exact reverese
    order that they got done.

    But in specific:
    Suppose class C is dependent on things in A. For the construction this
    is not a problem, since C is constructed after A has finished construction,
    so at the time the ctor of C runs, it can access all of A's functionality.

    But then destructin happens. Again C has to do work and wants to use information
    from A. If A gets destructed first, then that information will not be available
    any more. But if the destruction starts at C it can use everything from A needed
    to do its job.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Oct 31, 2003
    #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. frs
    Replies:
    20
    Views:
    794
    Alf P. Steinbach
    Sep 21, 2005
  2. arun
    Replies:
    2
    Views:
    577
    benben
    Jun 13, 2006
  3. Generic Usenet Account
    Replies:
    10
    Views:
    2,349
  4. Jimmy Hartzell
    Replies:
    0
    Views:
    440
    Jimmy Hartzell
    May 19, 2008
  5. Jimmy Hartzell
    Replies:
    2
    Views:
    1,184
    Jimmy Hartzell
    May 20, 2008
Loading...

Share This Page