Order of destructor calls

Discussion in 'C++' started by subramanian, Jan 4, 2007.

  1. subramanian

    subramanian Guest

    Suppose I have the following class:(shown only partially. Assume proper
    ctors and dtors):

    class Date
    {
    ...

    static Date temp_date;
    static Date another_date;

    ...
    };

    Date Date:temp_date;
    Date Date::another_date;

    int main(void)
    {
    ...

    return 0;
    }

    QUESTION:
    1)What will be the order of destructor calls for temp_Date and
    another_date.

    2)Does the Standard C++ define the order of evaluation of these objects
    in this situation or is it compiler dependent ?
    subramanian, Jan 4, 2007
    #1
    1. Advertising

  2. subramanian wrote:
    > Suppose I have the following class:(shown only partially. Assume
    > proper ctors and dtors):
    >
    > class Date
    > {
    > ...
    >
    > static Date temp_date;
    > static Date another_date;
    >
    > ...
    > };
    >
    > Date Date:temp_date;
    > Date Date::another_date;
    >
    > int main(void)
    > {
    > ...
    >
    > return 0;
    > }
    >
    > QUESTION:
    > 1)What will be the order of destructor calls for temp_Date and
    > another_date.
    >
    > 2)Does the Standard C++ define the order of evaluation of these
    > objects in this situation or is it compiler dependent ?


    You're supposed to do your own homework, no? What book on C++
    are you reading that doesn't explain construction/destruction of
    static objects?

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jan 4, 2007
    #2
    1. Advertising

  3. subramanian

    subramanian Guest

    Firstly I am a beginner in C++. I am posted this question for learning
    purpose. This is not a homework question because I am at home only,
    learning C and C++ and I am planning to take up a programmer's Job in
    India. I am reading Bjarne Stroustrup's "The C++ Programming Language
    2nd edition and 3rd edition".

    So kindly clarify my doubt.
    subramanian, Jan 4, 2007
    #3
  4. subramanian

    Ian Collins Guest

    subramanian wrote:
    > Firstly I am a beginner in C++. I am posted this question for learning
    > purpose. This is not a homework question because I am at home only,
    > learning C and C++ and I am planning to take up a programmer's Job in
    > India. I am reading Bjarne Stroustrup's "The C++ Programming Language
    > 2nd edition and 3rd edition".
    >
    > So kindly clarify my doubt.
    >

    Please provide the context you are replying to. Reading the above,
    there isn't a doubt to clarify.

    --
    Ian Collins.
    Ian Collins, Jan 4, 2007
    #4
  5. subramanian

    Ian Collins Guest

    subramanian wrote:
    > Suppose I have the following class:(shown only partially. Assume proper
    > ctors and dtors):
    >
    > class Date
    > {
    > ...
    >
    > static Date temp_date;
    > static Date another_date;
    >
    > ...
    > };
    >
    > Date Date:temp_date;
    > Date Date::another_date;
    >
    > int main(void)
    > {
    > ...
    >
    > return 0;
    > }
    >
    > QUESTION:
    > 1)What will be the order of destructor calls for temp_Date and
    > another_date.
    >

    Implemenentation defined.

    --
    Ian Collins.
    Ian Collins, Jan 4, 2007
    #5
  6. "Ian Collins" <> wrote in message
    news:...
    > Implemenentation defined.
    >
    > --
    > Ian Collins.


    Only the construction order of different translation units is implementation
    defined. Within a single translation units, all nonlocal stored objects are
    constructed in order of _definition_ (and destructed in reverse order).

    - Sylvester Hesp
    Sylvester Hesp, Jan 4, 2007
    #6
  7. subramanian

    Ondra Holub Guest

    subramanian napsal:
    > Suppose I have the following class:(shown only partially. Assume proper
    > ctors and dtors):
    >
    > class Date
    > {
    > ...
    >
    > static Date temp_date;
    > static Date another_date;
    >
    > ...
    > };
    >
    > Date Date:temp_date;
    > Date Date::another_date;
    >
    > int main(void)
    > {
    > ...
    >
    > return 0;
    > }
    >
    > QUESTION:
    > 1)What will be the order of destructor calls for temp_Date and
    > another_date.
    >
    > 2)Does the Standard C++ define the order of evaluation of these objects
    > in this situation or is it compiler dependent ?


    In your example Date:temp_date will be created before
    Date::another_date and destruction will proceed in reverse order. It
    depends on order in which you will write it, so if you change it to

    Date Date::another_date;
    Date Date::temp_date;

    the order of creation and destruction changes too. It is guaranteed by
    standard for case that all such statements are in one translation unit
    (that usualy means in one source file). if you place Date::another_date
    in one source file and Date::temp_date in another source file, the
    order of construction and destruction is not guaranteed at all. It may
    be different in different cases even in one compiler.
    Ondra Holub, Jan 4, 2007
    #7
    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. Honne Gowda A
    Replies:
    2
    Views:
    860
    Karl Heinz Buchegger
    Oct 31, 2003
  2. frs
    Replies:
    20
    Views:
    735
    Alf P. Steinbach
    Sep 21, 2005
  3. arun
    Replies:
    2
    Views:
    534
    benben
    Jun 13, 2006
  4. Zongjun  Qi
    Replies:
    3
    Views:
    311
    Salt_Peter
    Nov 15, 2006
  5. Jimmy Hartzell
    Replies:
    0
    Views:
    411
    Jimmy Hartzell
    May 19, 2008
Loading...

Share This Page