首页 > 解决方案 > 如何在 C++ 中模拟覆盖父函数(不隐藏)?

问题描述

从阅读这个答案(Is it possible to override a function in C++ child class without using virtual keyword to the function in parent class which is abstract?)很明显,不能从子类中覆盖父函数。

不过,我需要这样的功能。这是一个非功能性设置,描述了我正在尝试做的事情。

class Parent {
public:
  Graph createGraph() {
    return new Graph([this](float x){ 
        return this->getY(x); 
    });
  }

  float getY(float x) {
    return sin(x);
  }
}

.

class Child: public Parent {
public:
  float getY(float x) {
    return x * x;
  }
}

我的设置的重要部分是我有一个父类,它有一个函数,该函数始终引用一个经常被子类重载的函数。来自 Java/Javascript 领域,我的方法是做你在上面看到的,但似乎我对 c++ 的考虑不正确。

我如何模拟(即获得相对相似的功能)这种形式的覆盖?

我知道通过不干燥,我可以复制/粘贴createGraph到两者中,它会起作用。如果这是一个有经验的 c++ 开发人员会这样做的方式,那对我来说已经足够了。但是现在,我正在寻找一种方法来解决这个问题,这种方法与我的更像 java 的方式一样 DRY。

编辑:这里的核心问题似乎是我误解了什么virtual,假设这意味着父类中可能没有函数的定义(类似于其他语言中的抽象函数)。情况并非如此,virtual 似乎做了其他允许抽象类的事情,但并不需要它们。

标签: c++inheritanceoverriding

解决方案


使用 CRTP 模式。https://gcc.godbolt.org/z/J_N5Y_

void sink(int);
template<class ChildT>
struct Parent {
    void doStuff(){
        sink(
            static_cast<ChildT*>(this)->getY()
            );
    }
    int getY() {
        return 42;
    }
};
struct Child : Parent<Child> {
    int getY() {
        return 43;
    }
};
struct Child2 : Parent<Child2> {
//Does not want to customize, default is fine.
};
void foo() {
    Child c;
    c.doStuff(); # passes 43
    Child2 c2;
    c2.doStuff(); # passes 42
}

推荐阅读