我写过这个小程序:
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!5First!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!5First!3
有人可以向我解释为什么会这样吗?
解决方法 当使用虚函数调度时,所谓的“最终覆盖”就是被调用的东西.对于甚至覆盖继承的虚函数的函数,它必须满足一些条件:If a virtual member function
vf
is declared in a classBase
and in a
classDerived
,derived directly or indirectly fromBase
,a member
functionvf
with the same name,parameter-type-List (8.3.5),
cv-qualification,and refqualifIEr (or absence of same) asBase::vf
is
declared,thenDerived::vf
is also virtual (whether or not it is so
declared) and it overrIDesBase::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 functionvoID 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) { ^总结
以上是内存溢出为你收集整理的c – 具有不同参数类型的虚函数全部内容,希望文章能够帮你解决c – 具有不同参数类型的虚函数所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)