c++ - 覆盖派生类上的虚函数并仍然调用基本功能

标签 c++ inheritance virtual derived

class Base
{
    public:
    void virtual func() {   int i = 0; cout << "i"; }
}

class Derived: public Base
{
    public:
    void func() {  .....******   }
}

在派生类中,当您从派生对象调用 func 时,当 func 已被覆盖时,您如何仍然拥有基本功能。你能直接调用 Base.func() 吗?先谢谢

最佳答案

您可以使用 :: 从派生类函数调用重写的基类虚函数。

Base::func()

关于c++ - 覆盖派生类上的虚函数并仍然调用基本功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19578837/

相关文章:

c++ - 有什么办法可以知道C++的派生类是否实现了非纯虚函数?

c++ - 'unbounded_buffer' 中的消息数

c++ - 关于虚函数的代价

c++ - 谷歌测试 : "char-array initialized from wide string"

inheritance - 何时使用 Dart 中的接口(interface)?

c++ - 直接type cast derived到基类调用重载函数

javascript - 原型(prototype)复制 vs Object.create() vs new

c# - 有没有办法在 C# 中只向方法添加代码而不完全覆盖它?

c++ - 为什么我们需要需要?

c++ - 使用 CMake 包含共享库 (.so)