Reputation: 3388
Suppose I have a base class foo
class foo {
virtual int get() const = 0;
}
and maybe 20 sub-classes foo_1, foo_2 ... inherited from foo and in the form of:
class foo_1 : public foo {
int get() const { return 1; }
}
...
class foo_20 : public foo {
int get() const { return 20; }
}
Suddenly life is not so easy! I have a class foo_21, which has to do this:
class foo_21 : public foo {
int get() { member_ = false; return 0; }
bool member_;
}
The problem is get is declared as const in the base class, and I have to change something in the sub-class foo_21. How could I find a way to go around it?
Upvotes: 0
Views: 2103
Reputation: 476990
Your base function isn't virtual
, which makes all this highly speculative. Your code should already be working as you posted it (though maybe not as you expect).
You could use mutable
members:
class foo_21 : public foo
{
int get() const { member_ = false; return 0; }
mutable bool member_;
};
It's important that the mutable variable doesn't affect the logical constness of your class. If it does, you should rework your design.
Upvotes: 7