考虑一个简单的继承类:
class Base
{
void func() {
cout << "base" << endl;
}
};
class Derived : public Base
{
void func() {
cout << "derived" << endl;
}
};如果我运行Derived::func(),我会得到
derived我想修改这段代码以获得
base
derived更类似于扩展而不是重写的东西。
我已经能够用构造函数得到类似的东西,但不能用普通函数。
非常感谢,卢西奥
发布于 2012-04-17 00:17:40
class Derived : public Base
{
void func() {
Base::func(); // Call the base method before doing our own.
cout << "derived" << endl;
}
};发布于 2012-04-17 00:17:30
要从派生类访问基类函数,您可以简单地使用:
Base::func();在您的示例中,应该将其作为func()的派生实现的第一行。
发布于 2018-04-05 12:50:58
使用Base::func();是正确的,就像前面提到的其他几个一样。
记住,基类的构造函数总是首先被调用,然后才是派生类的构造函数。
https://stackoverflow.com/questions/10177809
复制相似问题