Re: assigmnet operator when class data members are immutable
by Francis Glassborow <francis.glassborow@[EMAIL PROTECTED]
>
Mar 30, 2008 at 11:43 AM
chuck wrote:
> Hello,
> I have a class whose data members are constant. So, default assignment
> will not happen. I am trying to write and assignment operator method
> that will allow assignment.
>
> I think my assignment with the swap might be the right direction but I
> am currrently stumped.
> Can anyone help with this assignment operator?
Why on Earth would you want to mess with a cl***** invariants in such a
way. Note that the compiler 'knows' that the member variables are
constant and can (in its view) cache them for use elsewhere. IOWs it
does not need to go back to the object to look up the current value. So
if you lie to the compiler and change it anyway your code is
fundamentally flawed.
This follows a basic rule that any time you declare a variable as const
at top level any attempt to change its value leads to undefined behaviour.