65,211
社区成员
发帖
与我相关
我的任务
分享#include <windows.h>
#include <stdio.h>
class Test{
public:
static bool g_bInited;
Test();
~Test();
void GlobalInit(){printf("全局初始化完成,其它此类对象无须再次初始化。\r\n");};
void GlobalUninit(){printf("全局清除完成,其它此类对象工作时如果g_bInited==false会发生异常。\r\n");};
void Working();
};
Test::Test()
{
if(g_bInited==false)
{
GlobalInit();
g_bInited=true;
}
}
Test::~Test()
{
if(g_bInited==true)
{
GlobalUninit();
g_bInited=false;
}
}
void Test::Working()
{
if(g_bInited==true)
printf("工作正常\r\n");
else
printf("我还正在工作,别人却把g_bInited变为假了,工作异常\r\n");
}
bool Test::g_bInited = false;
Test g_t;
void main()
{
int i=0;
while(i++<100)
{
g_t.Working();
if(i==5)
{
Test t;
t.Working();
}
Sleep(1000);
}
class Test{
public:
static bool g_bInited;
static bool g_bQuit;
static void SetQuit(bool bRes){g_bQuit = bRes;}
Test();
~Test();
void GlobalInit(){printf("全局初始化完成,其它此类对象无须再次初始化。\r\n");};
void GlobalUninit(){printf("全局清除完成,其它此类对象工作时如果g_bInited==false会发生异常。\r\n");};
void Working();
};
Test::Test()
{
if(g_bInited==false)
{
GlobalInit();
g_bInited=true;
}
}
Test::~Test()
{
if (g_bQuit==true)
{
if(g_bInited==true)
{
GlobalUninit();
g_bInited=false;
}
}
}
void Test::Working()
{
if(g_bInited==true)
printf("工作正常\r\n");
else
printf("我还正在工作,别人却把g_bInited变为假了,工作异常\r\n");
}
bool Test::g_bInited = false;
Test g_t;
void main()
{
int i=0;
Test::SetQuit(false); // 不允许清除
while(i++<100)
{
g_t.Working();
if(i==5)
{
Test t;
t.Working();
}
Sleep(1000);
}
Test::SetQuit(true); // 允许清除
}
#include <windows.h>
#include <stdio.h>
class Test{
public:
static bool g_bInited;
Test();
~Test();
void GlobalInit(){printf("È«¾Ö³õʼ»¯Íê³É£¬ÆäËü´ËÀà¶ÔÏóÎÞÐëÔٴγõʼ»¯¡£\r\n");};
void GlobalUninit(){printf("È«¾ÖÇå³ýÍê³É£¬ÆäËü´ËÀà¶ÔÏó¹¤×÷ʱÈç¹ûg_bInited==false»á·¢ÉúÒì³£¡£\r\n");};
void Working();
};
Test::Test()
{
if(g_bInited==false)
{
GlobalInit();
g_bInited=true;
}
}
Test::~Test()
{
if(g_bInited==true)
{
GlobalUninit();
g_bInited=false;
}
}
void Test::Working()
{
if(g_bInited==true)
printf("¹¤×÷Õý³£\r\n");
else
printf("ÎÒ»¹ÕýÔÚ¹¤×÷£¬±ðÈËÈ´°Ñg_bInited±äΪ¼ÙÁË£¬¹¤×÷Òì³£\r\n");
}
bool Test::g_bInited = false;
Test* g_t=new Test();
void main()
{
int i=0;
while(i++<100)
{
g_t->Working();
if(i==5)
{
Test *t=new Test();
t->Working();
}
Sleep(1000);
}
}