Reputation: 2567
The code is :
#include <iostream>
using namespace std;
class A
{
public:
A() { cout << "A::A" << endl; }
~A() { cout << "A::~" << endl; }
};
class B
{
public:
B() { cout << "B::B" << endl; }
~B() { cout << "B::~" << endl; }
};
int main()
{
B b;
static A a;
return 0;
}
The output is :
B::B
A::A
B::~
A::~
The scope of non-static object b
and the scope of static object a
ends at the end of main()
function.
Question : Why is the order of constructors is same as the order of destructors ?
Upvotes: 2
Views: 248
Reputation: 122350
As addition to already existing answers, lets take a more phenomenological approach. Consider a small change on your example:
void foo() {
B b;
static A a;
}
int main() {
foo();
foo();
}
I assume you know that a
is initialized only once and on the second call we get the exact same object a
, because it is declared as static
. Hence, it cannot be destroyed during or directly after the first call to foo
. The expected first part of the output is
B::B // first call to foo()
A::A
B::~
B::B // second call to foo()
// no second call to A::A !
B::~
a
can only be destroyed when your program terminates, otherwise you could not "reuse" it when calling the function again (thats why I had to modify the example, you cannot call main
). As other answers explain in great detail, this happens after main
returns. Hence last line of output will be
A::~
Upvotes: 0
Reputation: 14589
Because they have different lifespans. A
is declared as function-local static variable.
Object created by declaration of automatic function-local variable got lifespan which begins before any use of that object and ends with the most nested code block (braces block) containing that declaration. In your case that's function main()
body.
Object created by declaration of static function-local variable begins to exist after execution flow had entered the most nested code block containing that declaration and before any use of that object.
It got process-wide life span (stops to exist at std::atexit()
), which happens after the function main()
will be exited.
SO they are created in this particular case in order of declaration, but A
will get destroyed way later. If your function was called twice, you'd see that B
would be created twice but A
only once. If function's flow would somehow omit either declaration, by if()
statement or by returning early, the order of their creation of would change or both may be omitted.
It's implementation specific detail, but usually destruction of function-local statics implemented in same way as destruction of global objects, by call to a library function that lies under implementation of std::atexit, adding the address of destructor bound with value of pointer to object itself, but the execution may be concurrent (or may be not) with result of user's calls of std::atexit
.
Upvotes: 3
Reputation: 172924
Static local variables will be destroyed at program exit.
The destructor for a block-scope static variable is called at program exit, but only if the initialization took place successfully.
So b
will be destoryed firstly at the end of main()
, a
will be destroyed after that.
For the initialization,
are initialized the first time control passes through their declaration
So b
will be initialized firstly in main()
, then a
gets initialized.
Upvotes: 6