BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / cpp / #30802同步于 2009/11/3
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖

菜鸟求解答,关于那个adobe的笔试题 22 22...

behappy
2009/11/3镜像同步3 回复
#include <iostream> using namespace std; class a { public: virtual int foo(int *const index){cout<<"2\n";return 0;} virtual int foo(int *index)const{cout<<"1\n";return 0;} protected: int value; }; class b:public a { public: int foo(int *const index){cout<<"22\n";return 0;} int foo(int *index)const{cout<<"11\n";return 0;} }; void main() { b ta; a* p = &ta; const a* q = &ta; int index = 3; int * ptr_index = &index; int * const ptr_index_const = &index; p->foo(ptr_index); p->foo(ptr_index_const); q->foo(ptr_index); q->foo(ptr_index_const); getchar(); } 这个输出22 22 11 11 如果把int foo(int *const index){cout<<"22\n";return 0;} 注释掉 输出 2 2 11 11 如果把virtual int foo(int *const index){cout<<"2\n";return 0;}也注释掉 输出11 11 11 11 对吗?
订阅后,新回复会通过你的通知中心匿名送达。
3 条回复
Eager机器人#1 · 2009/11/3
【 在 behappy 的大作中提到: 】 : #include <iostream> : using namespace std; : class a : ................... 是的。 但是不明白 为什么 屏蔽掉 a中的2后 输出是:11 11 11 11 在此基础上 屏蔽掉11 输出是:1 1 1 1
behappy机器人#2 · 2009/11/3
转一下原帖中的回帖: 发信人: devc (BeThereForYou), 信区: CPP 标 题: Re: adobe的笔试题,结果为啥是22 22 发信站: 北邮人论坛 (Tue Nov 3 16:09:03 2009), 站内 。。。。。。 可见,不管形参如何,类的非常量成员会优先调用非常量成员函数(如果继承类没有实现则会到基类里寻找如果基类里还没有有则再回到继承类里调用常量成员函数),而类的常量成员只能调用常量成员函数(不能调用非常量函数)。可以自己屏蔽实现 试一试。 我是这么理解的。。。
devc机器人#3 · 2009/11/3
呵呵 我也是 这么理解滴...不过 以后 再找找 原理性的说明 吧 还是... [em18] 【 在 behappy 的大作中提到: 】 : 转一下原帖中的回帖: : 发信人: devc (BeThereForYou), 信区: CPP : 标 题: Re: adobe的笔试题,结果为啥是22 22 : ...................