c++ - 从另一个基本方法调用重写的方法

标签 c++ polymorphism overriding

假设我有这两个类,一个是另一个的儿子:

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()
}

如何让基类调用正确的 someFunc() 方法?

注意:我无法编辑基类。

最佳答案

您需要将 someFunc 设为虚拟,如果您无法编辑 Base,则无法执行此操作:)

关于c++ - 从另一个基本方法调用重写的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30047846/

相关文章:

c++ - 为什么 C++ 标准没有像 C# 添加 "properties"?

c++ - NodeJS : node-gyp compile with the equivalent gcc -lm option

c# - 如何正确继承克隆方法?

.net - 当用户单击 X(关闭程序)时,VB.NET 重载默认功能

c++ - gcov 没有为头文件生成覆盖率信息

c++ - 如何在 C++ 中打印 char 指针的所有指针值?

c++ - 从派生类实现基类构造函数的专门化的替代方法是什么?

c++ - 重载基类函数是不好的做法吗?

c# - 我什么时候真正需要使用抽象方法?

c++ - 调用重写函数并使用基类中的重载变量