I understand that static variables are initialized at compile time, but what about a static object?
e.g. if I have the following code:
class A {
A();
};
A::A(){
std::cout << "Constructing A" << std::endl;
}
int main(){
std::cout << "Hello World!" << std::endl;
static A A_obj;
std::cout << "Goodbye cruel world" << std::endl;
return 0;
}
Should I expect the output to be:
Hello World!
Constructing A
Goodbye cruel world
or
Constructing A
Hello World!
Goodbye cruel world
"I understand that static variables are initialized at compile time"
Not true. static
variables at function scope are strictly initialised at the point of first encounter. And will be destructed after the closing brace of main
.
You'll get output in this order:
Hello World!
Constructing A
Goodbye cruel world
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With