Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old January 4th, 2007, 05:25 AM
subramanian
Guest
 
Posts: n/a
Default Order of destructor calls

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

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles