I've been searching for a solution to this for ages, and can't seem to be finding anything relevant.
The situation:
I have a class B, which is a child of class A. It overrides the function foo() in A.
The problem:
Is there any way I can set things up so that when the foo() of B is called, it would also automatically call its parent function in A?
Some code to better illustrate the problem:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
class A {
public:
void foo() {
cout << "PARENT\n";
}
};
class B: public A {
public:
void foo() {
cout << "CHILD\n";
}
};
int main() {
B test;
test.foo(); //output is "CHILD"; need it to be "PARENT CHILD" w/o calling the parent function manually inside the child
}
|
Essentially I am looking for a constructor-like inheritance for these functions, where every parent constructor is called
automatically before the child one. I want the exact same effect for these functions.
On a side-note, I am aware that you can call it manually from the child with
A::foo();
. What I am looking for however is the parent method being called automatically.
This one has had me stumped for quite some time now. Any ideas would be greatly appreciated.