BCB 单例模式报错???
class Singleton {
public:
static Singleton& Instance() {
static Singleton theSingleton;
return theSingleton;
}
/* more (non-static) functions here */
private:
Singleton(); // ctor hidden
Singleton(Singleton const&); // copy ctor hidden
Singleton& operator=(Singleton const&); // assign op. hidden
~Singleton(); // dtor hidden
};
外部调用时报错
int _tmain(int argc, _TCHAR* argv[])
{
Singleton::Instance().fn();
return 0;
}
[BCC32 Error] File2.cpp(17): E2166 Destructor for 'Singleton' is not accessible
求大虾们赐教啊!!!
[解决办法]
单例模式最简单的实现:
class Singleton
{
public:
static Singleton *GetInstance()
{
if (m_pInstance == NULL)
m_pInstance = new Singleton();
return m_pInstance;
}
private:
Singleton() {}
static Singleton *m_pInstance;
};
Singleton *Singleton::m_pInstance = NULL;