被一个有关虚函数的问题困扰,请大家帮忙看一下。谢了。
#include <iostream>using namespace std;
class Base{
public:
virtual Base * afn(){cout<<"In Base class\n";
return this;
}
};
//
class Sub:public Base{
public:
virtual Sub * afn(){cout<<"In Sub class\n"; //line 12;
return this;
}
};
//
void test(Base & x){Base * b; b=x.afn();}
//
int main(){
test(Base());
test(Sub());
return 0;
}
这是一本书上的源码,并称x.afn()具有多态行为,运行结果为:
In Base class
In Sub class
可在VC++6下总编译不过,.cpp(12) : error C2555: 'Sub::afn' : overriding virtual function differs from 'Base::afn' only by return type or calling convention请问一下是各个编译器的处理不同么?