http://www.devx.com/tips/Tip/12476
Once declared virtual, a member function can be overridden in any level of derivation, as long as the overriding version has the identical signature of the original declaration. This is quite straightforward, but sometimes a virtual has to return an object of type that differs from the return type declared in its base class. The C++ Standard allows that only when the return type is replaced with a class publicly derived from the original return type. For example:
class B {
virtual B* clone () const { return new B; }
}
class D {
void* clone () const { return new B; } //error; return type
//differs
D* clone () const { return new D; } //OK, D is publicly
//derived from B
}
本文介绍了 C++ 中虚函数的特性,特别是当虚函数的返回类型在派生类中发生变化的情况。根据 C++ 标准,只有当派生类的返回类型是基类返回类型的公有派生时,这种变化才是允许的。

被折叠的 条评论
为什么被折叠?



