C++ Logo

std-proposals

Advanced search

[std-proposals] Derived class's function invokes base class's function

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Wed, 6 Apr 2022 16:34:55 +0100
Sometimes we write code like this:

#include <iostream>
using std::cout;
using std::endl;

class Base {
public:
    void Method(void)
    {
        cout << "Base must do something essential here" << endl;
    }
};

class Derived : public Base {
public:
    void Method(void)
    {
        this->Base::Method();
        cout << "Derived has a job to do here" << endl;
    }
};

int main(void)
{
    Derived object;

    object.Method();
}

This program prints:

        Base must do something essential here
        Derived has a job to do here

I think we should have a way of getting the method in the Derived
class to automatically invoke the base class's method. So instead of
having:

class Derived : public Base {
public:
    void Method(void)
    {
        this->Base::Method();
        cout << "Derived has a job to do here" << endl;
    }
};

We would use the 'continue' keyword as follows:

class Derived : public Base {
public:
    void Method(void) continue
    {
        cout << "Derived has a job to do here" << endl;
    }
};


If the Derived class has more than one base class (including virtual
inheritance), then the method called "Method" is invoked for every
base class that has such a method -- this is where the 'continue'
keyword will be a lot more useful, for example if we had:

        class Derived : public Base1, public Base2, public Base3,
public base4 { . . . };

then using the 'continue' keyword would generate the following code:

        this->Base1::Method():
        this->Base2::Method():
        this->Base3::Method():
        this->Base4::Method():

What I am proposing here would work for any kind of method
(irrespective of whether it's virtual or not). That is to say, the
keyword 'continue' can be used alongside 'virtual' and 'override'.

Received on 2022-04-06 15:35:08