直接上代码和运行结果截图。
代码:
#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;
}
运行结果: