I read a line in Meyers: "A member function that modifies what a pointer points to frequently doesn`t act const. But if only the pointer is in the object, the function is bitwise const, and compilers wont complain."
I fail to understand that modifying a pointer in a function cannot maintain its bitwise constantness since its a member variable...
Even if we assume that bitwise constantness is only for values that pointers point to and not for the pointer addresses themselves.. Then why does it matter if its the only member variable in the class or if its not the only only member variable..
Basically this means that if you had
struct Foo
{
int bar;
};
you couldn't have a const member function change the value of bar
.
However if bar
is a pointer to an int
, you could change the value of the int in a const method because the int
is not actually part of the struct.
Both versions achieve the same goal (i.e. change the value of the int
) but in the first version you are breaking bitwise constness and the compiler will complain, in the second it wont.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With