Order of destructor calls

S

subramanian

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 ?
 
V

Victor Bazarov

subramanian said:
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
 
S

subramanian

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

Ian Collins

subramanian said:
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.
 
I

Ian Collins

subramanian said:
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.
 
S

Sylvester Hesp

Ian Collins said:
Implemenentation defined.

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
 
O

Ondra Holub

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top