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;
}
In this case how to deal with singleton and pure virtual function together.
If I'll make like m_selfInstance = new der();
things works fine.
But the real problem will occur if there are more derived classes
like der_1
and der_2
at that time the istance will be m_selfInstance = new der();
instead of new der_1()
or new der_2()
.
Please guide me how to go ahead with this.