Is it OK to return a const reference to a private member?

Returning a const & is a sensible thing to do in many circumstances, particularly if the object being returned is large or cannot be copied.

Regarding the const_cast, remember the "private" access specifier in C++ is there as an aid to the programmer - it is not intended to be a security measure. If someone wants access to an object's private members, it can get them, no matter what you try to do to prevent it.


Is it safe to return a const reference to a private member

Yes as long as the lifetime of the reference does not exceed the lifetime of the object which returned it. If you must expose the private member you do not want modified, this is a good way to do so. It's not foolproof but it's one of the better ways to do so in C++

Is it possible to use const_cast to actually mess around with member

Yes and there is nothing you can do to prevent this. There is no way to prevent someone from casting away const in C++ at any time. It's a limitation / feature of C++.

In general though, you should flag every use of const_cast as a bug unless it contains a sufficiently detailed comment as to why it's necessary.