c++ - 如何在 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 似乎做了其他允许抽象类的事情,但并不需要它们。
解决方案
使用 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
}
推荐阅读
- django - Django - 如何创建文件并将其保存到 ModelSerializer 中的字段
- yocto - 如何在 qemu 中运行 yocto 开发的树莓派镜像?
- ssh - ssh v2 最大压缩 (xzip/7zip)
- azure - 在更新 Nuget 时触发使用 Nuget 的项目的构建
- android - 存储文件在签名的 APK 中不起作用,但在调试 APK 中起作用
- javascript - 为什么 trim() 没有删除我的 Vue 应用程序中的前导空格?
- angular - TypeError:无法读取未定义 Ionic 3 的属性“toLowerCase”
- javascript - 如何检测 React 功能组件之外的点击
- node.js - 未设置“on data”事件侦听器时,nodejs http 模块会做什么?
- python - 如何在python中创建堆叠的条形图