5

Possible Duplicate:
How to force child same virtual function call its parent virtual function first

I have a class hierarchy where each derived class overrides a given virtual function and starts its implementation by calling the one in its parent class. The goal is to have each of the derived implementation to be executed, but I do not like the way I do it.

For example, I have this class:

class base
{
public:
  void do_stuff() { do_something(); }
  virtual void do_something() { }
};

Then I derive this class on several levels:

class derived_10:
  public derived_9 // which inherit from derived_8 and so on until derived_0
                   // which inherit from base
{
public:
  virtual void do_something()
  {
    // this will also call derived_8::do_something() and so on
    // until base::do_something()
    derived_9::do_something();

    // then, some stuff
  }
};

I'm looking for a solution that will make sure that all derived_x::do_something() will be called in sequence when base::do_stuff() is called, without having to expect the derived classes to do this themselves. Do you have an idea of the best way to get this behavior ?

Community
  • 1
  • 1
Julien
  • 2,139
  • 1
  • 19
  • 32
  • 4
    Why is your inheritance hierarchy so deep? – Cameron Apr 24 '12 at 19:34
  • It's too bad this question got closed as a dup. I was about to post an answer to this. This comment will have to do. How about something like [this](http://codepad.org/MtRoQ6Bg)? – greatwolf Apr 24 '12 at 19:49
  • @cameron, the hierarchy describes game objects like word_item <- renderable_item <- character <- monster <- playable_character <- hero. It does not have a depth of 10 classes but it still becomes quite large. – Julien Apr 25 '12 at 15:48
  • @victor-t your solution calls the super method in each derived class, which is the code I want to avoid. As you can see in the hierarchy in the comment to Cameron, the class hierarchy does not look like a class inheriting from itself several times and cannot easily be transformed like it. – Julien Apr 25 '12 at 15:53
  • @julien The only way to get that effect is if C++ has some mechanism that auto-executes a body of code + cascade without explicitly calling it. The closest thing I can think of that has those properties in C++ are constructors and destructors. But inheritance sounds like the wrong tool for the job here from how you're describing your problem. For example, is a hero really a monster? Your class hierarchy is failing Liskov's substitution. – greatwolf Apr 25 '12 at 19:28

1 Answers1

3

I've already asked a very similar question before: Calling overriden class methods as a chain in C++

The answer I've accepted pointed at your own solution. I can give you an idea about an alternative though. Constructors and destructors already have this behavior in C++, you might want to consider restructuring your code, so that the work is done during the construction or the destruction of an object that belongs to a class in a hierarchy. I'm not sure you'll be able to make this worth the effort though. On the other hand, you never know what you can get out of some template metaprogramming + some preprocessor magic.

Community
  • 1
  • 1
enobayram
  • 4,650
  • 23
  • 36