My sample code is as follows:
class Base {
public:
Base() { callVirtualfunc(); }
virtual void funca() = 0;
void callVirtualfunc() { funca(); }
};
// -----------------------------------------
class Derived : public Base{
public:
Derived() : Base() {}
void funca() { cout<<"Hello world"<<endl; }
};
I understand that I cannot call pure virtual function inside the constructor/destructor. I am calling them inside a function callVirtualfunc()
.
I am getting :: C++ runtime abort: an undefined pure virtual function was called
.
I am doing this to:
- Enforce all the derived classes to implement
funca()
. - I also want to guarantee derived classes to call
funca()
.
I am not able to understand my mistake here please help. Is there a workaround to achieve what I want.