如何设计一个含GetInstance()函数的类
直接上代码:
头文件(MyClass.h):
class CMyClass
{
public:
CMyClass(void);
~CMyClass(void);
public:
static CMyClass* GetInstance();
private:
static CMyClass* m_pMyClass;
};
CPP文件(MyClass.cpp):
#include "MyClass.h" CMyClass* CMyClass::m_pMyClass = NULL;
CMyClass::CMyClass(void)
{
} CMyClass::~CMyClass(void)
{
if (NULL != m_pMyClass)
{
delete m_pMyClass;
m_pMyClass = NULL;
}
} CMyClass* CMyClass::GetInstance()
{
if (NULL == m_pMyClass)
{
m_pMyClass = new CMyClass();
}
return m_pMyClass;
}
为什么需要GetIntance(),与new方法的区别?
答案参考:http://blog.csdn.net/yong199105140/article/details/8425781