📄 static objects 的构造和析构顺序.txt
字号:
* 1.本程序对应于thinking in c++ p409 的“static object destructors ”。
* 2.本程序在与说明全局的static object 和 局部于函数的 static object的构造和析构顺序
*3.记住书上说:P411。In c++, the constructor for a global static object is called before
* main() is entered, and destroyed as main() exits.
*4. the constructor of a local static object is called only if the function that includes its definition
* is called(如 f1() 中的 对象x1,也就是说如果f1()没有被调用则对象x1不会被创建);
*5.p409 like ordinary destructors ,destruction of static objects occurs in the reverse order of initialization.
*/
#include<iostream>
using namespace std;
class X
{
public:
X( int ii = 0 ) : m_i( ii ) { cout << "构造对象:x" << m_i << endl; }
~X(){ cout << "析构对象x" << m_i << endl; }
private:
int m_i;
};
void f()
{
static X x1( 1 );
}
X x2( 2 ); // global ( static storage )!
X x3( 3 ); // global ( static storage )!
int main()
{
cout << "welcome to main() " << endl;
f();
cout<< "exiting main()" << endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -