虚函数,请教
#include<iostream.h>
#include<stdio.h>
class Base
{
public:
virtual Base* afn()
{
cout<<"This is Base class\n";
return this;
}
};
class SubClass:public Base
{
public:
SubClass* afn()
{
cout<<"This is SubClass\n";
return this;
}
};
void test(Base& x)
{
Base * b;
b=x.afn();
}
void main()
{
Base bc;
SubClass sc;
test(bc);
test(sc);
}
错误如下
C:\vc\c++\Cpp4.cpp(19) : error C2555: 'SubClass::afn' : overriding virtual function differs from 'Base::afn' only by return type or calling convention
C:\vc\c++\Cpp4.cpp(6) : see declaration of 'Base'
Error executing cl.exe.
Cpp4.exe - 1 error(s), 0 warning(s)