Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bitiwise Const and pointers:

Tags:

c++

pointers

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..

like image 967
Cpp crusaders Avatar asked Mar 18 '23 03:03

Cpp crusaders


1 Answers

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.

like image 102
Borgleader Avatar answered Apr 01 '23 03:04

Borgleader