Automatically calling overriden parent method from child class

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.
Last edited on
This is not directly supported in C++.

Why can't you just call A::foo()? The obvious solutions are often the best.


An alternative is this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class A{
public:
  void foo() {
    cout << "PARENT\n";
    dofoo();
  }

protected:
  virtual void dofoo() { }
};

class B : public A {
protected:
  virtual void dofoo(){
    cout << "CHILD\n";
  }
};

int main()
{
  B test;
  test.foo();
}
Why can't you just call A::foo()?

It's a game project, so I'll have far more than two levels of inheritance.

In my particular case, I have a BaseObject class, that has an onFrame() method called every frame, which handles stuff shared by every on-screen object in the game (velocity, etc.). Sprite inherits from BaseObject, and adds animation events to the onFrame() method. BaseNPC inherits from Sprite and adds basic AI & pathfinding routines to onFrame(), and so on..

Essentially the effect I wanted was that I would only have to call onFrame() in the main game loop once for every object, and that would call every parent routine automatically. At least now I know it's not supported, so I'll probably just have a list of pointers to functions to call every frame for every object.

I somehow don't like the idea of having to call the parent method manually in every child function, seems like it may go really wrong once I have more levels of inheritance.

Anyway, thanks for the reply! :]
Last edited on
It's a game project, so I'll have far more than two levels of inheritance.


So?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Top
{
virtual void foo() { cout << "stuff"; }
};

class Middle : public Top
{
virtual void foo() { Top::foo(); cout << "more stuff"; }
};

class Bottom : public Middle
{
virtual void foo() { Middle::foo(); cout << "even more stuff"; }
};


As long as each class calls only it's direct parents, it's very maintainable.

so I'll probably just have a list of pointers to functions to call every frame for every object.


I don't see how you could possibly think function pointers would be easier/less messy than the above. I highly recommend you rethink this decision.

I somehow don't like the idea of having to call the parent method manually in every child function, seems like it may go really wrong once I have more levels of inheritance.


I don't particularly recommend it either. Especially not for what you're describing.

Really -- your best bet here is to just call parent functions explicitly.


EDIT: erp -- I misread that -- I thought you were talking about what I posted. I don't recommend doing what I posted in my first reply.

Calling parent functions explicitly is a little inconvenient, granted. However It's the least evil of all options available to you. Other approaches will get much more complicated and will be harder to maintain as the project grows.

With this approach you only have to worry about your immediate parents. However deep you go doesn't matter
Last edited on
True, you have a point there.

Was just really hooked on the idea of having it all called for me without having to worry about the lower level in the children methods.

The function list seemed like it would support multiple methods for any class called on-frame, but that's sort of pointless now that I think of it. You can just call any extra methods from the main onFrame function.

Guess I'll go for calling them explicitly. Thanks for the help!
Last edited on
Topic archived. No new replies allowed.