int B::f(const B& other) const
不override http://en.cppreference.com/w/cpp/language/virtual#In_detail int A::f(const A& other) const
因为参数类型不一样。那么就不会通过调用来调用f()
基于基类的引用A
.
如果某个成员函数 vf 在 a 中被声明为 virtual
类 Base 和一些派生类,直接派生或派生
间接地,从 Base 具有成员函数的声明
相同的
name
parameter type list (but not the return type)
cv-qualifiers
ref-qualifiers
那么Derived类中的这个函数也是virtual的(无论是还是
其声明中未使用关键字 virtual)并且覆盖Base::vf(无论其中是否使用了 override 一词
宣言)。
如果你使用覆盖说明符 http://en.cppreference.com/w/cpp/language/override(C++11 起)编译器将生成错误。
class B : public A {
public:
int f(const A& other) const { return 2; }
virtual int f(const B& other) const override { return 3; }
};
Such as Clang http://rextester.com/BOIMX66399:
source_file.cpp:10:17: error: 'f' marked 'override' but does not override any member functions
virtual int f(const B& other) const override { return 3; }
^
如果您在基类中为其添加重载,您可能会得到您想要的。请注意,类的前向声明B
将需要。
class B;
class A {
public:
virtual int f(const A& other) const { return 1; }
virtual int f(const B& other) const { return 1; }
};
LIVE http://rextester.com/KRAWT94911