📄 p409函数中static类成员只初试化一次.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 + -