overriding virtual function, can the return type be either a pointer or a reference?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • johnwang
    New Member
    • Feb 2010
    • 2

    overriding virtual function, can the return type be either a pointer or a reference?

    code example from an opensource game:


    Code:
    namespace CEGUI{
    class CEGUIEXPORT Renderer
    {
    public:
         virtual TextureTarget* createTexture() = 0;
    
    }
    }
    
    
    class cFake_Renderer : public CEGUI::Renderer
    {
    public:
        virtual	CEGUI::Texture *createTexture(void) { return NULL; };
    }


    in vs2008 ,it cause a compile error:

    cFake_Renderer: :createTexture overriding virtual function return type differs and is not covariant from 'CEGUI::Rendere r::createTextur e'

    does the author write a wrong code ?
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    You are trying to compare these 2 functions

    Renderer::creat eTextureTarget
    cFake_Renderer: :createTexture

    What every error you are getting in the override cFake_Renderer: :createTexture it has nothing to do with the declaration of Renderer::creat eTextureTarget. Notice they actually have different function names.

    Comment

    • johnwang
      New Member
      • Feb 2010
      • 2

      #3
      sorry,it should be:
      virtual TextureTarget* createTexture() = 0; and
      virtual CEGUI::Texture *createTexture( void) {...}

      i have corrected the code above

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        The return type of an overriding virtual function can be different from the return type of the function being overriden provided that the two return types are partof the same inheritance hierarchy. This C++ covariance.

        In all other cases the overriding function must have exctly the same function prototype as the function it overrides.

        Comment

        Working...