class base{
static base* m_selfInstance;
public:
static base* GetInstance();
virtual void abc() = 0;
};
class der:public base{
public:
void abc(){cout << "Derived...\n";}
};
base* base::m_selfInstance = NULL;
base* base::GetInstance()
{
if(m_selfInstance == NULL)
{
/*Throws error here and it's natural, because it's violates C++ standard*/
m_selfInstance = new base();
}
return m_selfInstance;
}
int main()
{
base *b = base::GetInstance();
//b->abc();
system("pause");
return 0;
}
在这种情况下如何一起处理单例和纯虚函数。
如果我会像m_selfInstance = new der();
那样做得很好。
但是,如果当时有more derived classes
der_1
和der_2
,那么真正的问题就会出现,其中,m_selfInstance = new der();
代替new der_1()
或new der_2()
}。
请指导我如何继续这样做。