⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 static objects 的构造和析构顺序.txt

📁 里面的代码是自己写的,参考书是thingking in c++,代码有详细的说明,对学习c++语法非常有帮助!
💻 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 + -