Reputation: 1350
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
Reputation: 917
class Base
{
public:
virtual void someFunc() { cout << "Base::someFunc" << endl; }
void someOtherFunc() {
cout << "Base::someOtherFunc" << endl;
someFunc(); //calls Base:someFunc()
};
Upvotes: 0
Reputation: 13318
You would need to make someFunc
virtual which you can't do if you can't edit Base
:)
Upvotes: 3