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
likeder_1
并且der_2
那个时候 istance 将m_selfInstance = new der();
不是new der_1()
or new der_2()
。请指导我如何继续进行。