单选题
C++
2017-03-30
已知类XX中声明了如下的公有虚函数:
virtual void f( )const;
XX的派生类YY重定义了这个虚函数,XX和YY都有默认的构造函数,且有如下定义:
YY yy;
XX xx,* px=&xx,&rx=yy,*pp=&yy;
则下列对函数f的调用中,属于非多态调用的是( )。
A.px->f( )
B.rx.f( )
C.XX.f( )
D.PP->f( )
参考答案
C
答案解析
C++中,多态调用必须满足两个条件:被调用的成员函数是虚函数;必须通过基类类型的引用或指针进行函数调用。题意中f()函数是虚函数,满足条件之一,所以只需要看调用对象是引用或指针。px是XX类型的指针,通过px调用f()函数,满足多态性,选项A正确;rx是XX类型的引用,通过rx调用f()函数,满足多态性,选项B正确;PP是XX类型的指针,通过pp调用f()函数,满足多态性,选项D正确;xx是XX类型的对象,通过xx调用f()函数,不满足多态性,选项C错误;本题答案为C选项。
你可能感兴趣的试题
A.int GetPrice( ){return price;}
A.int GetPrice( ){return price;}
A.int GetPrice( ){return price;}
B.int GetPrice( )const{return price;}
B.int GetPrice( )const{retum price;}
B.int GetPrice( )const{retum price;}
C.int Piano::GetPrice( ){return price;}
C.int Piano::GetPrice( ){return price;}
C.int Piano::GetPrice( ){return price;}
D.int Piano::GetPrice( )const{return price;}
D.int Piano::GetPrice( )const{retum price;}
D.int Piano::GetPrice( )const{retum price;}
A.类Boy是类Girl的友元类
B.函数Display是类Girl的友元函数
C.类Boy的成员函数Print是类GM的友元函数
D.类Boy的成员函数Show中不能访问类cirl的私有成员height