gpt4 book ai didi

C++ 扩展继承函数

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:05:06 24 4
gpt4 key购买 nike

考虑一个简单的继承类:

class Base 
{
void func() {
cout << "base" << endl;
}
};

class Derived : public Base
{
void func() {
cout << "derived" << endl;
}
};

如果我运行 Derived::func() 我得到

derived

我想修改这段代码得到

base
derived

比覆盖更类似于扩展。

我已经能够使用构造函数获得类似的东西,但不能使用普通函数。

非常感谢, 卢西奥

最佳答案

class Derived : public Base
{
void func() {
Base::func(); // Call the base method before doing our own.
cout << "derived" << endl;
}
};

关于C++ 扩展继承函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10177809/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com