Connecting Tech Pros Worldwide Help | Site Map

Order of destructor calls

  #1  
Old January 4th, 2007, 05:25 AM
subramanian
Guest
 
Posts: n/a
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 ?

  #2  
Old January 4th, 2007, 05:45 AM
Victor Bazarov
Guest
 
Posts: n/a

re: Order of destructor calls


subramanian wrote:
Quote:
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


  #3  
Old January 4th, 2007, 05:55 AM
subramanian
Guest
 
Posts: n/a

re: Order of destructor calls


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.

  #4  
Old January 4th, 2007, 06:45 AM
Ian Collins
Guest
 
Posts: n/a

re: Order of destructor calls


subramanian wrote:
Quote:
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.
  #5  
Old January 4th, 2007, 06:45 AM
Ian Collins
Guest
 
Posts: n/a

re: Order of destructor calls


subramanian wrote:
Quote:
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.
  #6  
Old January 4th, 2007, 11:55 AM
Sylvester Hesp
Guest
 
Posts: n/a

re: Order of destructor calls


"Ian Collins" <ian-news@hotmail.comwrote in message
news:503lu3F1e9nr5U1@mid.individual.net...
Quote:
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


  #7  
Old January 4th, 2007, 12:25 PM
Ondra Holub
Guest
 
Posts: n/a

re: Order of destructor calls



subramanian napsal:
Quote:
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.

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
gcc, class forward declarations and destructor calls Juha Nieminen answers 3 March 5th, 2007 04:55 PM
GC destructor calls mupp answers 6 November 16th, 2005 01:20 AM
Q: Order of construction for objects Jakob Bieling answers 15 July 23rd, 2005 05:53 AM
Sequence of constructor calls and destructor calls. Honne Gowda A answers 2 July 19th, 2005 08:50 PM