C++ Logo

std-proposals

Advanced search

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

From: Tony V E <tvaneerd_at_[hidden]>
Date: Fri, 15 Apr 2022 10:49:21 -0400
Your base<->derived interface should be separate from your public interface. http://www.gotw.ca/publications/mill18.htm



Sent from my BlackBerry portable Babbage Device
  Original Message  
From: Frederick Virchanza Gotham via Std-Proposals
Sent: Wednesday, April 6, 2022 11:35 AM
To: std-proposals_at_[hidden]
Reply To: std-proposals_at_[hidden]
Cc: Frederick Virchanza Gotham
Subject: [std-proposals] Derived class's function invokes base class's function

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'.
-- 
Std-Proposals mailing list
Std-Proposals_at_[hidden]
https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2022-04-15 14:49:23