0

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.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
attu013
  • 31
  • 7
  • possible duplicate of [pure virtual function with implementation](http://stackoverflow.com/questions/2089083/pure-virtual-function-with-implementation) – OMGtechy Feb 23 '14 at 13:44
  • 1
    You cannot call virtual functions (of derived classes) from a constructor, period! – πάντα ῥεῖ Feb 23 '14 at 13:48
  • 3
    You've misunderstood the 'rule'. The rule is that you cannot call a pure virtual function from a constructor [or destructor] directly _or indirectly_ for the object being created [or destroyed]. The extra indirection that you have here makes no difference. – CB Bailey Feb 23 '14 at 13:48
  • 1
    The extra indirection only serves to confuse the compiler so it doesn't give you a better diagnostic, but you're still a violator. – Kerrek SB Feb 23 '14 at 13:49
  • Thanks for replying guys... IS there a workaround for my problem? – attu013 Feb 23 '14 at 13:51
  • How can I achieve these objectives 1) Enforce all the derived classes to implement funca(). 2) I also want to guarantee derived classes to call funca(). ???? Please do help – attu013 Feb 23 '14 at 13:53
  • For workarounds, see e.g. http://www.parashift.com/c%2B%2B-faq-lite/calling-virtuals-from-ctor-idiom.html I'd advise using a factory function (second approach mentioned in that C++ FAQ) – dyp Feb 23 '14 at 13:54
  • 1
    You have a design problem but it's unclear exactly what it is because your example is too simplified. If the derived classes can implement `funca` any way they choose why does it matter to the `Base` implementation that it is called - after all it might do nothing? If it's important to the construction of a derived class, then let the derived class call it (or another initializing function). – CB Bailey Feb 23 '14 at 13:55
  • 1
    @attu013 _'IS there a workaround for my problem?'_ Yes, you might consider using rather [static polymorphism](http://en.wikipedia.org/wiki/Template_metaprogramming#Static_polymorphism) than dynamic as in your sample. This will allow you to call anything from your derived classes anywhere doing a simple `static_cast<>`. – πάντα ῥεῖ Feb 23 '14 at 14:03
  • @CharlesBailey thanks I think your suggestion of using another initializing function is described in the link dyp posted. Thanks for helping me out. – attu013 Feb 23 '14 at 14:18
  • @πάνταῥεῖ will investigate your suggestion thanks for helping me. – attu013 Feb 23 '14 at 14:19

1 Answers1

1

According to the C++ Standard

4 Member functions, including virtual functions (10.3), can be called during construction or destruction (12.6.2). When a virtual function is called directly or indirectly from a constructor or from a destructor, including during the construction or destruction of the class’s non-static data members, and the object to which the call applies is the object (call it x) under construction or destruction, the function called is the final overrider in the constructor’s or destructor’s class and not one overriding it in a more-derived class. If the virtual function call uses an explicit class member access (5.2.5) and the object expression refers to the complete object of x or one of that object’s base class subobjects but not x or one of its base class subobjects, the behavior is undefined.

and

6 Member functions can be called from a constructor (or destructor) of an abstract class; the effect of making a virtual call (10.3) to a pure virtual function directly or indirectly for the object being created (or destroyed) from such a constructor (or destructor) is undefined

So in your example when the base class constructor will be called it in turn will call its pure virtual function not the overriden function of the derived class. And according to the second quote of the Standard the behaviour of the program in such a case is undefined.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335