c++ - How to use singleton and pure virtual function together? -
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 , 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 case how deal singleton , pure virtual function together. if i'll make m_selfinstance = new der();
things works fine. real problem occur if there more derived classes
der_1
, der_2
@ time istance m_selfinstance = new der();
instead of new der_1()
or new der_2()
. please guide me how go ahead this.
Comments
Post a Comment