C ++虚函数complie错误

时间:2015-12-09 04:43:07

标签: c++ polymorphism virtual ref

#include <iostream>
using namespace std;

class A {
public:
 virtual void f4() { cerr << "A::f4()" << endl; }
}

class B : public A {
public:
 virtual void f4(int n) { cerr << "B::f4(" << n << ")" << endl; }
}

class C : public B {
public:
 virtual void f4(int n = 1) { cerr << "C::f4(" << n << ")" << endl; }
}

int main() {
    A a;
    B b;
    C c;

    A& rab = b;     
    A& rac = c;     
    B& rbc = c;     

    rab.f4(); rac.f4(); rbc.f4(); 
}

当我运行时尝试使用 rab.f4();编译此代码rac.f4();主方法中的rbc.f4(); rbc.f4()给了我一个编译错误,它不会让我。谁能告诉我原因?

0 个答案:

没有答案