关于类成员函数中静态变量的一点提示
2014-11-07 10:57
218 查看
直接上代码和运行结果截图。
代码:
#include <iostream>
#include <string>
using namespace std;
class ClassOfMemberFuctionStaticVar{
public:
void printInfo()
{
static bool bFirstPrint=true;
string infoToPrint="";
infoToPrint=bFirstPrint?"This is the first time printing the info.":"This is not the first time printing the info.";
cout<<infoToPrint<<endl;
bFirstPrint=false;
}
};
int main()
{
cout << "Hello world!" << endl<<endl;
ClassOfMemberFuctionStaticVar *testObj=new ClassOfMemberFuctionStaticVar();
testObj->printInfo();
cout<<endl;
testObj=new ClassOfMemberFuctionStaticVar();
testObj->printInfo();
return 0;
}
运行结果:
代码:
#include <iostream>
#include <string>
using namespace std;
class ClassOfMemberFuctionStaticVar{
public:
void printInfo()
{
static bool bFirstPrint=true;
string infoToPrint="";
infoToPrint=bFirstPrint?"This is the first time printing the info.":"This is not the first time printing the info.";
cout<<infoToPrint<<endl;
bFirstPrint=false;
}
};
int main()
{
cout << "Hello world!" << endl<<endl;
ClassOfMemberFuctionStaticVar *testObj=new ClassOfMemberFuctionStaticVar();
testObj->printInfo();
cout<<endl;
testObj=new ClassOfMemberFuctionStaticVar();
testObj->printInfo();
return 0;
}
运行结果:
相关文章推荐
- C++关于this指针、静态变量和静态成员函数 http://www.cnblogs.com/clrmas/archive/2010/03/23/1692893.htm
- C++关于this指针、静态变量和静态成员函数
- 关于C++类的静态成员函数
- 关于成员函数和有元函数
- 关于c++的静态数据(函数)成员的理解
- 关于用于静态变量实现singleton的多线程互斥的一点疑问
- 每天学一点 (2):几个DC的常用类 和成员函数
- 关于fopen函数中路径名使用字符串的一点看法
- VC++学习笔记(关于CDC的成员函数CreateCompatibleDC(HDC hdc))
- 关于类成员函数指针
- 关于string的成员函数substr
- 关于类成员函数指针的调用问题
- 类成员函数的静态变量
- 给初学者关于eclilse汉化及tomcat错误的一点提示
- 关于accept函数需要注意的一点
- 关于在C++内用指针调用类的成员函数
- 转:关于在C++内用指针调用类的成员函数
- 多线程函数和静态线程函数调用类成员非静态变量和函数的问题
- 关于Java类设计中信息隐蔽原则的一点提示
- 关于项目类库的重构和函数名的一点见解