/**结论:派生类override基类的虚函数时,函数的返回值也必须一致。
*/
#include <iostream>
using namespace std;
class CBase
{
public:
virtual void Func(){ cout<<"CBase::Func()...."<<endl; }
};
class CDerived: public CBase
{
public:
void Func(){ cout<<"CDerived::Func()...."<<endl; }
/**下面的override函数的返回值不同导致编译不过:
* error C2555: “CDerived::Func”: 重写虚函数返回类型有差异,且不是来自“CBase::Func”的协变
*/
//int Func(){ cout<<"CDerived::Func()...."<<endl; return 0; }
};
int main(int argc,char* argv[])
{
CBase* base = new CDerived();
base->Func();
system("pause");
return 0;
}
override函数的返回值也必须一致
最新推荐文章于 2024-05-31 18:06:20 发布