我试图理解虚函数是如何工作的,我陷入了某个角色.
我写过这个小程序:
class First
{
public:
Virtual void f(int a)
{
cout << "First!" << endl;
cout << a << endl;
}
};
class Second : public First
{
public:
void f(int a) {
cout << "Second!" << endl;
cout << a << endl;
}
};
void main() {
Second s;
First *p = &s;
p->f(5);
First n;
p = &n;
p->f(3);
_getch();
}
此代码导致:
Second!
5
First!
3
但是,如果我将Second :: f()函数中的int更改为其他类型,如下所示:
class First
{
public:
Virtual void f(int a) {
cout << "First!" << endl;
cout << a << endl;
}
};
class Second : public First
{
public:
void f(double a) { //double instead int here!
cout << "Second!" << endl;
cout << a << endl;
}
};
void main() {
Second s;
First *p = &s;
p->f(5);
First n;
p = &n;
p->f(3);
_getch();
}
我的程序从不调用Second :: f(),我得到的结果如下:
First!
5
First!
3
有人可以向我解释为什么会这样吗?
当使用虚函数调度时,所谓的“最终覆盖”就是被调用的东西.对于甚至覆盖继承的虚函数的函数,它必须满足一些条件:
If a Virtual member function vf is declared in a class Base and in a
class Derived, derived directly or indirectly from Base, a member
function vf with the same name, parameter-type-list (8.3.5),
cv-qualification, and refqualifier (or absence of same) as Base::vf is
declared, then Derived::vf is also Virtual (whether or not it is so
declared) and it overrides Base::vf.
– ISO / IEC 14882:2001(E)§10.3(大胆强调我的)
很简单,在你的第二个例子中,Second :: f(double)的参数列表与First :: f(int)的参数列表不同,所以Second :: f(double)不是(自动)虚拟的并且不会覆盖First: :F(INT).
C 11关键字覆盖声明了一个方法覆盖继承的虚方法的意图,以便编译器可以告诉您何时不这样做.例如,如果你这样做了:
void f(double a) override {
编译器会给你这个诊断通知你它实际上没有覆盖任何东西,它甚至告诉你为什么它没有(“在第一个参数类型不匹配(‘int’与’double’)”):
main.cpp:15:18: error: non-Virtual member function marked 'override' hides Virtual member function
void f(double a) override { //double instead int here!
^
main.cpp:7:14: note: hidden overloaded Virtual function 'First::f' declared here: type mismatch at 1st parameter ('int' vs 'double')
Virtual void f(int a) {
^