Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static initialization of object

Tags:

c++

I have a global object which is declared before main function and a static object inside main.

  • Which one of these uses (or do both?) static initialization?
  • I've heard that A obj1 is also called static; why is that?


class A { ... };

A obj1;

int main()
{
    static A obj2;
}
like image 980
user103214 Avatar asked Sep 09 '26 22:09

user103214


1 Answers

obj1 has static storage. It will be initialized when the program starts.

obj2 also has static storage because you said so. It will be initialized when main() executes the first time.

like image 129
David Heffernan Avatar answered Sep 12 '26 13:09

David Heffernan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!