6
votes

Consider the following 2 programs.

#include <iostream>
using std::cout;
class Base {
    public:
        virtual void f()=0;
        void g() {
            f();
        }
        virtual ~Base() { }
};
class Derived : public Base
{
    public:
    void f() {
        cout<<"Derived::f() is called\n";
    }
     ~Derived() {}
};
class Derived1 : public Base
{
    public:
    void f() {
        cout<<"Derived1::f() is called\n";
    }
    ~Derived1() { }
};
int main() {
    Derived1 d;
    Base& b=d;
    b.g();
    b.f();
}

Compiles & runs fine and gives expected outcome..

#include <iostream>
using std::cout;
class Base {
    public:
        virtual void f()=0;
        Base() {
            f();    // oops,error can't call from ctor & dtor
        }
};
class Derived : public Base
{
    public:
        void f() {
            std::cout<<"Derived::f() is called\n";
        }
};
int main() { Derived d; Base& b=d; b.f(); }

The above program fails in compilation. Why is it allowed to call a pure virtual function from the member function of the same class in which the pure virtual function is declared? Is it fine to do this or is it undefined behavior because derived class still doesn't provide an implementation of the pure virtual function? Why can't a pure virtual function be called from constructor & destructor of the same class? I know that Derived class constructors can call pure virtual functions of the base class. What does the C++ standard say about this?

2
@vsoftco: that link discusses different topic.Destructor
the second answer addresses something very similar, not the same, that's why I said related.vsoftco

2 Answers

7
votes

"Why pure virtual function can't be called from constructor ... ?"

Because the final class isn't constructed completely at this point, and the vtable isn't completely setup, to dispatch the function call correctly.


You may alternatively use a static relationship for base and derived class like proposed with the CRTP:

template<class DerivedType>
class Base {
    public:
        void g() {
            static_cast<DerivedType*>(this)->f();
        }
        virtual ~Base() { }
};

class Derived : public Base<Derived>
{
    public:
    void f() {
        cout<<"Derived::f() is called\n";
    }
     ~Derived() {}
};

class Derived1 : public Base<Derived1>
{
    public:
    void f() {
        cout<<"Derived1::f() is called\n";
    }
    ~Derived1() { }
};
1
votes

Why it is allowed to call pure virtual function from the member function of same class in which pure virtual function is declared?

Because this is technically feasible and used in practice: see the Template Method pattern.

Why pure virtual function can't be called from constructor & destructor of the same class?

This is technically not straight-forward to implement in C++ (no vtable yet). But more fundamentally, you should not need it anyway since you always know the exact class of your object when calling a constructor.