class ICompiler
{
virtual void fn1() = 0;
virtual void DestroyInstance() = 0;
};
class CCompiler:public ICompiler
{
public:
static CCompiler *CreateInstance()
{
return new CCompiler;
}
private:
CCompiler();
virtual ~CCompiler();
public:
virtual void fn1();
virtual DestroyInstance()
{
delete this;
}
};
ICompiler *pCompiler = NULL;
pCompiler = CCompiler::CreateInstance();
if (pCompiler == NULL)
return;
pCompiler->fn1();
pCompiler->DestroyInstance();
class CMyBase
{
public:
CMyBase();
~CMyBase();
protect:
ICompiler *GetComplier()
{
if (m_pCompiler == NULL)
{
m_pCompiler = CCompiler::CreateInstance();
}
return m_pCompiler;
}
public:
void func();
private:
ICompiler *m_pCompiler;
};
class CMyApp:public CMyBase
{
public:
CMyApp();
~CMyApp();
public:
void func()
{
ICompiler *pCompiler = GetCompiler();
}
};//定义结构或类的后面需加;