Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does a derived class need to implement a method as const?

Tags:

c++

oop

constants

Why doesn't CC::onError need be const in the below example? BB:onError has to be const else compiler error.

BB is required to implement onError as const else compiler error.

CC can be implemented as non-const. Why?

class AA  {
public:
    AA() {};
    virtual ~AA() {};

    // On any error
    virtual void onError(int32_t error) const = 0;
};

class BB : public AA {
public:
    BB() {};
    virtual ~BB() {};

    virtual void onError(int32_t error) const {
        //changing x is invalid here due to const
        //method required to be const else compiler error
    }
protected:
    int x;
};

class CC : public BB {
public:
    CC() {};
    virtual ~CC() {};

    virtual void onError(int32_t error)  {
        x = 5;
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    CC c;
    return 0;
}
like image 437
dizzy Avatar asked Mar 11 '26 15:03

dizzy


1 Answers

Why doesn't CC::onError need be const in the below example? 

Because a member function isn't required to be const.

The problem is that you expect it overrides A::onError and that's why C++11 introduced override keyword.
It's a subtle error, but the fact is that you are not overriding A::onError. You are declaring a new virtual member function in C that actually hides A::onError.
Try to use this:

class CC : public BB {
public:
    CC() {};
    virtual ~CC() {};

    virtual void onError(int32_t error) override  {
        x = 5;
    }
};

The compiler will tell you explicitly what's the error:

error: 'virtual void CC::onError(int32_t)' marked 'override', but does not override

On the other side, the following code would be correct (at least it overrides A:onError as expected) but for the fact that it doesn't compile for you can't modify x being onError const:

virtual void onError(int32_t error) const override  {
    x = 5;
}

Unless, of course, you define x as mutable:

mutable int x;

I wouldn't recommend it, for mutable is meant for slightly different cases, but it works.

like image 68
skypjack Avatar answered Mar 13 '26 05:03

skypjack