Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enforce third party methods virtuality

Tags:

c++

virtual

I'm extending a class provided by a third part library. The class, let's call it Foo, has a reset() method which can be called in order to restart Foo's behavior. The reset() method is also used internally by the class.

class Foo
{
  public:
    void reset () {
        /* ... */
    }
    void something () {
        reset();
    }
};

So far, I need to overload the reset() method in order to reset my additional features as well:

class Bar : public Foo
{
  public:
    void reset() {
        /* ...something... */
        Foo::reset();
    }
};

Unfortunately, since the Foo::reset() method is not virtual, by calling Bar::something() I get the Foo::reset() method called instead of Bar::reset().

Is there a way (different from overloading Foo::something() as well) to make it backward-virtual?

like image 627
Dacav Avatar asked Aug 31 '10 15:08

Dacav


1 Answers

You cannot extend classes that were not intended to be extended.

like image 72
David Rodríguez - dribeas Avatar answered Sep 24 '22 19:09

David Rodríguez - dribeas