有没有一些方法,使单例类初始化上的程序堆栈(因此成员变量也)? 我曾尝试以下,两者都失败了:
1)
class CStack{
public:
void* getAlloc(long);
static CStack& Instance(){
static CStack theStack;
return theStack;
}
private:
bool _data[100];
CStack(){};
CStack(const CStack&);
CStack& operator=(const CStack&);
};
2)
class CStack{
public:
void* getAlloc(long);
static CStack* Instance();
private:
CStack(){};
CStack(CStack const&){};
CStack& operator=(CStack const&){};
static CStack* m_pInstance;
};
CStack* CStack::m_pInstance = NULL;
CStack* CStack::Instance(){
if (!m_pInstance) // Only allow one instance of class to be generated.
m_pInstance = new CStack;
return m_pInstance;
}
第一失败,由于非配置新的初始化(m_pInstance =新CSTACK),第二由于延迟初始化。 可以,请帮助我的人?
谢谢