Luca
Luca

Reputation: 1350

Call overriden method from another base method

Say i have these two classes, one son of the other:

class Base
{
public:
    void someFunc() { cout << "Base::someFunc" << endl; }
    void someOtherFunc() { 
        cout << "Base::someOtherFunc" << endl;
        someFunc(); //calls Base:someFunc()
    };

class Derived : public Base
{
public: 
    void someFunc() { cout << "Derived::someFunc" << endl; }
};

int main()
{
    Base b* = new Derived();
    b->someOtherFunc();    // Calls Base::someOtherFunc()
}

How can i make the base class call the right someFunc() method?

Note: i can't edit Base class.

Upvotes: 2

Views: 50

Answers (2)

CreativeMind
CreativeMind

Reputation: 917

class Base
{
public:
    virtual void someFunc() { cout << "Base::someFunc" << endl; }
    void someOtherFunc() { 
        cout << "Base::someOtherFunc" << endl;
        someFunc(); //calls Base:someFunc()
    };

Upvotes: 0

Drax
Drax

Reputation: 13318

You would need to make someFunc virtual which you can't do if you can't edit Base :)

Upvotes: 3

Related Questions