Last active
July 1, 2017 13:44
-
-
Save DamonHao/a93a5d3f65d39104b4a1 to your computer and use it in GitHub Desktop.
C++ 类的static成员变量和函数内static变量初始化时机
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| C++ static 变量初始化时间就和普通static的初始化时间是一样的。 | |
| */ | |
| #include <iostream> | |
| using namespace std; | |
| class A | |
| { | |
| public: | |
| A(){cout<<1<<endl;} | |
| }; | |
| class B | |
| { | |
| public: | |
| static A a; | |
| }; | |
| A B:: a; //有了这一句静态变量a才会初始化 | |
| int main() | |
| { | |
| // B b; | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| C++ 函数内static 变量的初始化发生在第一次调用函数的时候。 | |
| */ | |
| #include <iostream> | |
| using namespace std; | |
| class A | |
| { | |
| public: | |
| A(){cout<<1<<endl;} | |
| }; | |
| void func() | |
| { | |
| static A a; | |
| } | |
| int main() | |
| { | |
| func(); | |
| func(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment