Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Q: order and rules of static/global objects destruction

0 views
Skip to first unread message

DasI

unread,
Jan 23, 2002, 4:55:23 AM1/23/02
to
Hello,

Here i have an example code:

------------------------------------
#include <iostream.h>
#include <stdlib.h>

class A
{
public:
A(){cout << "c-tor A" << endl;}
~A(){cout << "d-tor A" << endl;}
};

class B
{
public:
B(){cout << "c-tor B" << endl;exit (-1);}
~B(){cout << "d-tor B" << endl;}
};

class C
{
public:
C(){cout << "c-tor C" << endl;}
~C(){cout << "d-tor C" << endl;}
};

static A the_a;
static B the_b; // constructor calls exit();
static C the_c;

int main()
{
return 0;
}
---------------------------------------

And i compile it with:
gcc version 2.96 20000731 (Red Hat Linux 7.1 2.96-98)

and the output is:
c-tor A
c-tor B
d-tor C
d-tor B
d-tor A

As we see, all destructors called, but the_b abd the_c objects was'nt
initialised...

I tried the same code on WIN MS VC 6.0 and the output was:
c-tor A
c-tor B
d-tor A

Question: what behavior is correct and why ? Does C++ standard says
something about that? If so, could you please give me a reference.

I think code compiled with MS VC compiler behaves correctly. I
think destroing of objects should go in reverse order and destructors
should not be called for objects which constructor wasn't called or
failed.

Thanks in advance.
--
DasI

0 new messages