"KBill" <[EMAIL PROTECTED]> wrote:
>
> Hello,
> 
> void foo::p() const{    
> member = 1;                       // illegal    
> const_cast <int&> (member) = 1;   // a bad practice but legal 
> }
> 
> Where I got this from comments this as bad practice
> 
> const_cast <int&> (member) = 1
> 
> So does that mean if this is a template it is bad practice
> to use one this way?

It's not a template, it's a cast operator. It's effectively
removing the implied 'const' associated with member. [Hence
why direct assignment is illegal. Assuming member is an int.]

> Why is this bad practice?

Because you're lying to the compiler.

More specifically, the compiler is free to perform certain
operations (such as optimisations) based on the fact that
you've told the compiler that the function does not modify
members.

If you have members that need to be modifiable in const
member functions, you should give them mutable storage
class.

-- 
Peter

Reply via email to