Brandon
Brandon

Reputation: 23500

Overloading Base Class function

Ok I have a class that wraps all WinAPI controls to make them operate like Java/C#/.Net controls (yes I know there are libraries that do this already) and looks like:

class Control
{
    public:
          //Bunch of other stuff..

        virtual HWND GetHandle() const;
};

and a derived class that looks like:

class MenuBar: public Control
{
    public:
        //Bunch of other stuff..

        virtual HMENU GetHandle() const;


        enum class Flags
        {
            POPUP = MF_POPUP, STRING = MF_STRING
        };
};

However, the compiler gives me an error about wrong return type for overloading the Base Class' GetHandle function.

How can I overload the GetHandle to have a different return type and to make it ignore the Base class's implementation? I really like the name GetHandle() and didn't want to change it (though that is an option)..

Is there another way? I'm asking because perhaps there is a way to make it forget that the base class has an implementation?

Upvotes: 0

Views: 93

Answers (1)

Potatoswatter
Potatoswatter

Reputation: 137810

You need covariant return types. The return type from the derived class needs to be a pointer or reference derived from the return type of the base class.

This can be accomplished by wrapping HWND and HMENU in a class hierarchy making a parallel of the API's organization. Templates may help if it's all generic.

Upvotes: 2

Related Questions