Is it possible to return a reference to a member variable in such a way as to prevent it from being altered (thereby changing the state of the object)? The following code does not prevent this!
This does not stop someone from doing the following...Code:class X { }; class Y { public: const X& get () const { return _x; } private: X _x; };
Code:int main () { X x; Y y; const_cast<X&> (y.get ()) = x; }




Reply With Quote