#include<iostream> using namespace std; class P { public : void print() { cout << " Inside P" ; } }; class Q : public P { public : void print() { cout << " Inside Q" ; } }; class R: public Q { }; int main( void ) { R r; r.print(); return 0; } |
(A) 内P (B) 内部Q (C) 编译器错误:对print()的调用不明确 答复: (B) 说明: print函数不在类R中,因此在继承层次结构中查找它。print()在P类和Q类中都存在,应该调用哪一个?其思想是,如果存在多级继承,则在继承层次中线性搜索函数,直到找到匹配的函数。 这个问题的小测验
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END