th***@gmx.de wrote:
Thanks. Is there something
like mutable for methodes, too? Or does just a const_cast of this help
there?
Thanks,
Thomas
There is no such thing as:
class X
{
public:
void getY() mutable;
};
presumably getY() would be a method that can be called from const
functions even though it is not itself a const method (i.e. it may
modify anything).
Doesn't seem to make sense to have it there though as if it could
modify anything, your const methods shouldn't be calling it.
The only time it is useful to const_cast in such a situation is when
you have 2 overloads that use the same method to return an address or
reference to some class member, one of them const and one non-const.
Then you would normally implement one to call the other, and then it is
legitimate to use const_cast because it is YOUR implementation and you
know what you are doing (that you are not actually breaking any
contracts).
There are workarounds to that, eg having some external template method
that does the calculations where the template parameter can be T or
const T. Those who love to stick to coding standards and have a coding
standard that says "never const cast" would probably do it that way.
Practical programmers will often find the const_cast method the
clearest option for the situation.