基类和派生类成员函数返回值可以为各自的指针或者引用,但不可以为本身类类型:
#include <iostream>
#include <string>
class A
{
public:
virtual A& showSelf() =0;//ok
// virtual A* showSelf_1() =0;//ok
// virtual A showSelf_2() =0;//error invalid abstract return type 'A'
// A showSelf_3(); //error invalid abstract return type 'A'
};
class B:public A
{
public:
B& showSelf()
{std::cout << "B" << std::endl;return *this;} //ok
// B* showSelf()
// {std::cout << "B*" << std::endl;return this;} //ok
// B showSelf()
// {std::cout << "B" << std::endl;return *this;} //error
};
int main(void)
{
B b;
b.showSelf();
return 0;
}