DEV Community

loading...

Discussion on: When to use const in C++? Part II: member variables

Collapse
markboer profile image
Mark Boer

Personally I only like to add const to places where it improves readability. As it makes it more clear what the intent is of code. For example a const getter that returns a reference to a const object.

Also I believe the example you show might result in undefined behaviour:

$5.2.11/7 - Note: Depending on the type of the object, a write operation through the pointer, lvalue or pointer to data member resulting from a const_cast that casts away a const-qualifier68) may produce undefined behavior (7.1.5.1).

Interesting series though, keep it up!

Collapse
sandordargo profile image
Sandor Dargo Author

Thanks, indeed. My point was that it's really not worth to do such "magic" so that we can have const members when we also want to be able to copy.