Learning  C++
Home
Tutorials
C++  Programs
Contact  us
Sitemap
Your Ad Here
Your Ad Here
Virtual  Function  Return  Type

Generally,  the  redefined  version  of  a  virtual  function  must  have  the  same  return  type  as in  the  initial  declaration.  However,  for  certain  virtual  functions  in  a  base  class,  their overriding  version  in  a  derived  class  can  have  a  return  type  that  is  different  from  the overridden  function.  This  is  possible  only  if  the  derived  class ( say,  D )  function's  return type  is  D *  or  D &,  and  base  class  ( say,  B )  function's  return  type  is  B*  or  B &, respectively,  and  B  is  an  accessible  base  class  of  D.



For  example,

                                               class  B
                                               {
                                                         public :
                                                                       
                                                                    virtual  B *   F ( ) ;
                                                                    // . . . .
                                           
  } ;


                                               class  D :  public  B
                                               {
                                                         public  :
                                                                       virtual  D *  F ( ) ;
                                                                       // . . . .
                                             
 } ;