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

📄 p409函数中static类成员只初试化一次.txt

📁 里面的代码是自己写的,参考书是thingking in c++,代码有详细的说明,对学习c++语法非常有帮助!
💻 TXT
字号:
/* 本程序是thinking in c++ 原版 p409页的例题的演化;
* 本程序的目的是为了说明:在函数内部的定义的static 类对象只初试化一次
* 实验方法:将f()函数中的static 去掉试试看
*/
#include<iostream>
using namespace std;
class X 
{
public:
	X( int ii = 0 ) : m_i( ii ) { cout << "X::X() " << endl; }
	
	void increaseM_i() {  m_i++;}   
	
	void displayM_i() { cout << m_i << endl;}
	
	~X(){ cout << "X::~X()" << endl; }
private:
	int m_i;           //类的唯一成员
};

void f()
{
	static  X x2( 0 );     
	 x2.increaseM_i();    //对成员自增1
	 cout << "the value of the sole member in class X is : ";
	 x2.displayM_i();     //打印成员
}

int main()
{
	int index = 0;
	for( index = 0; index < 10; ++index)
		f();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -