On local and global static variables in C++

前端 未结 7 1091
野的像风
野的像风 2020-11-29 05:55

C++ Primer says

Each local static variable is initialized before the first time execution passes through the object\'s definition. Local statics are

7条回答
  •  情书的邮戳
    2020-11-29 06:25

    The main or most serious difference is time of initialization. Local static variables are initialized on first call to function where they are declared. The global ones are initialized at some point in time before the call to main function, if you have few global static variables they are intialized in an unspecified order, which can cause problems; this is called static initialization fiasco.

提交回复
热议问题