Covariant Returen Types (covariant return type)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.