The base class virtual Func Returns a Class (class super) of PTR or ref, and the subclass override of the virtual Func return type can be changed to PTR or ref for that class subclass (class Sub:public Super).
classbase{ Public: Virtualbase* Clone ()Const{return NewBase (* This); } Virtual~Base () {}};classDerived: Publicbase{ Public: Virtualbase* Clone ()Const{return NewDerived (* This); } Virtual~Derived () {}};intMain () {Derived D; Base* Bptr =D.clone (); Derived* DPtr = dynamic_cast<derived*>(BPTR); if(!dPtr) { DeletedPtr; DPtr=nullptr; //Throw Exception ... } return 0;}
Switch
......
class Derived: public base{ public : virtual derived* Clone () const {return new Derived (*this ); virtual ~derived () {}}; int main () {Derived D; Derived * dPtr = D.clone (); return 0 ;}
classCherry {};classBingcherry: PublicCherry {};classcherrytree{ Public: Virtualcherry* pick ()Const{return NewCherry ();}};classBingcherrytree: Publiccherrytree{ Public: Virtualbingcherry* pick ()Const{return NewBingcherry ();}};
Covariant Returen Types (covariant return type)