Different return value of an overridden class

Posted by Samer Afach on Stack Overflow See other posts from Stack Overflow or by Samer Afach
Published on 2012-04-08T11:16:39Z Indexed on 2012/04/08 11:28 UTC
Read the original article Hit count: 214

Filed under:
|
|
|
|

I have a simple but confusing question here. Is it legal to have a different return value type for overridden methods than the abstact ones defined in the base class?? I did that and the compiler didn't complain... could someone please explain?

class MyBaseClass
{
    int value;

public:
    virtual int getValue() = 0;
};

class MyClass : public MyBaseClass
{
    double value;

public:
    virtual double getValue(); // here!!! return is double, not int
};

double MyClass::getValue()
{
   return this->value;
}

The compiler totally accepted something similar (MSVC und MinGW)... could anyone please exaplain to what extent this is legal?

© Stack Overflow or respective owner

Related posts about c++

Related posts about class