C++: call pure virtual function from member function of same class
Asked Answered
C

2

7

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?

Carbylamine answered 23/5, 2015 at 17:31 Comment(3)
Related: https://mcmap.net/q/98310/-why-is-a-call-to-a-virtual-member-function-in-the-constructor-a-non-virtual-call/3093378Conductance
@vsoftco: that link discusses different topic.Carbylamine
the second answer addresses something very similar, not the same, that's why I said related.Conductance
M
8

"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() { }
};
Medicaid answered 23/5, 2015 at 17:34 Comment(2)
Yes ! And it's on purpose, because base class shall be constructed first.Carton
Nice! I think you can also use if(dynamic_cast<DerivedType*>(this)) dynamic_cast<DerivedType*>(this)->f(); for safety, as it will catch Base<Derived>* pBase = new Base<Derived>; pBase->f();, which otherwise will give you UB.Conductance
P
1

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.

Poche answered 23/5, 2015 at 17:39 Comment(2)
This is quite technically feasible - and broken. It's what Java does.Ellga
@Ellga Edited to take into account your comment.Treenatreenail

© 2022 - 2024 — McMap. All rights reserved.