#include <iostream>
using namespace std;
class test {
public:
test() : num(100) {
cout << "constructor " << num << endl;
}
test(int n) : num(n) {
cout << "constructor " << num << endl;
}
test(const test &other)
{
this->num = other.num;
cout << "copy constructor " << num << endl;
}
void Print()
{
cout << "num is " << num << endl;
}
public:
int num = 10;
};
void Func()
{
static test local(22);
local.Print();
}
test g_test(5);
test g_test1 = g_test;
int main() {
cout << "hello world" << endl;
test a(8);
a.Print();
test b;
b.Print();
g_test.Print();
static test st(11);
st.Print();
return 0;
}
输出:
constructor 5
copy constructor 5
hello world
constructor 8
num is 8
constructor 100
num is 100
num is 5
constructor 11
num is 11
结论:
全局变量在main函数之前初始化的;
局部静态变量是在第一次使用的时候初始化的,不使用则不会初始化。