[英]Calling overridden method of helper class in C++
我来自Python背景,目前我正在C++中学习OOP。
我在弄清楚如何让代码在继承到HelperChild
的助手 class HelperBase
中调用正确方法时遇到问题。
#include <iostream>
class HelperBase {
public:
HelperBase() {}
virtual void something() {
std::cout << "HelperBase" << std::endl;
}
};
class HelperChild : public HelperBase {
public:
HelperChild() {}
void something() {
std::cout << "HelperChild" << std::endl;
}
};
我在 class Base
中使用的HelperBase
class ,它被设置为成员变量。
class Base {
public:
Base(HelperBase &helperBase) : hb(helperBase) {}
virtual void print() {
std::cout << "-- Base" << std::endl;
hb.something();
}
HelperBase hb;
};
然后将此 class 用作 class Child
的基础 class :
class Child : public Base {
public:
Child(HelperChild &helperChild) : Base(helperChild) {
helperChild.something();
}
};
main
方法是
int main() {
HelperChild helperChild;
Child child(helperChild);
child.print();
return 0;
}
这将输出以下内容:
HelperChild
-- Base
HelperBase
为什么最后一行没有打印“HelperChild”? 我必须做哪些改变才能实现这一目标? (我不确定我是否以正确的方式使用了virtual
)。
编辑:在我试图弄清楚的实际情况下, Base::print
是一个非常大的方法,我不想在Child
class 中覆盖它。 我只想改变助手 class 的行为。
详细地说,您可以将复制构造函数视为“按值复制”,派生的 object 的每个值都将被复制以创建基本 object。 由于您的 Helper 类没有 class 成员,因此它没有复制任何内容。
此外 function 不可复制, C++ 通过vtable处理虚函数。 基础 class 将具有基础 class 的 vtable,这就是 hb.something() 称为基础版本的原因。
Base
有一个HelperBase
但没有派生的HelperChild
。class Base {
public:
Base(HelperBase &helperBase) : hb(helperBase) {} // hp(helperBase) means you still have a helper base.
virtual void print() {
std::cout << "-- Base" << std::endl;
hb.something();
}
HelperBase hb;
};
然后在main
中, child.print()
将调用属于HelperBase
的 hb.something() 。
要实现多态性,您需要一个指针来获取HelperChild
的实例。 它被称为依赖注入,我假设您正在尝试实现它。
class Base {
public:
Base(HelperBase &helperBase) {
hb = &helperBase;
}
virtual void print() {
std::cout << "-- Base" << std::endl;
hb->something();
}
HelperBase* hb;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.