c++ - 使用可选参数覆盖虚函数

标签 c++ inheritance virtual optional-arguments

为什么打印 23 作为输出;我的预期是 33。有人可以阐明这一点吗。

struct A {
    virtual void f() {cout << "1";}
};

/* Private inheritance */
struct B : private A {
    void f(int x = 0) {cout << "2";}
};

struct C : B {
    void f(){cout << "3";}
};

int main() {
    C obj;
    B &ref = obj;
    ref.f();
    obj.f();
}

最佳答案

B 结构中的 f(int x = 0) 方法不与 A 共享签名C 结构的 f() 方法。

关于c++ - 使用可选参数覆盖虚函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14930406/

相关文章:

C++ 模板类复制构造函数和赋值运算符

c++ - 为什么在读取时发现 eof 时会设置故障位?

c++ - C++中的继承和构造函数定义

java - 子类错误: CloneNotSupportedException never being thrown in the try body

c++ - 在 C++ 中,如果一个函数覆盖了一个虚函数,它会自动为虚函数吗?

java - 在模型更改时更新虚拟 TableViewer 的条目

c++ - 我想我已经重写了一个虚拟方法,但我仍然得到 : "X must implement the inherited pure virtual method Y"

c++ - 非推导上下文中模板参数推导的解决方法

c++ - 什么时候在网络编程中使用ntohs函数?

asp.net-mvc - Razor:如何强制文件夹中的 View ,从特殊类继承