标题:请教一个关于多态性的问题!!
取消只看楼主
mitain
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2005-10-11
 问题点数:0 回复次数:0 
请教一个关于多态性的问题!!
下面的程序的运行结果为什么会是那样的,哪位大虾能给我说说吗?谢谢! # include <iostream.h> class A { public: virtual void call(){cout<<"A::call()\n\n"<<endl;} }; class B:public A { public: void func() { cout<<"B::func()"<<endl; call(); } virtual void call(){cout<<"B::call()\n\n"<<endl;} }; class C:public B { public: virtual void call(){cout<<"C::call()\n\n"<<endl;} }; void main(void) { C my_c; C *mcp=new C; B my_b; cout<<"#1 testing"<<endl; my_c.func(); cout<<"#2 testing"<<endl; ((B *)(&my_c))->func(); cout<<"#3 testing"<<endl; mcp->func(); cout<<"#4 testing"<<endl; my_b.func(); } 运行结果是: #1 testing B::func() C::call() #2 testing B::func() C::call() #3 testing B::func() C::call() #4 testing B::func() B::call()
搜索更多相关主题的帖子: 多态 
2005-10-11 19:09



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-29891-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.252990 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved