C++中的类继承

loc*_*zed 2 c++ inheritance overloading class

下面的代码将从B类调用函数F,但有人可以向我解释为什么会这样.是否有可能来自B类的方法重载来自V类的方法(因为B是从V继承的)?提前致谢.

#include <iostream>
using namespace std;

class V{
public: 
    void f(){ x+=2; cout << "V:"<< x;};
    int x;
};
class B: public virtual V{
public:
    void f(){ x+=3; cout << "B:"<< x;};
    int x;
};
class D: public B, virtual public V{
public:
    void g(){   x++;    f();    }
};
void main(){
    D ins;
    ins.x = 1;
    ins.g();
}
Run Code Online (Sandbox Code Playgroud)

Alo*_*ave 6

B::f() 隐藏方法V::f(),这称为函数隐藏.

好读:
什么意思,警告:Derived :: f(char)隐藏Base :: f(double)?