Why is it legal for a class to have a public member function that returns a reference to a private member? Even though this would have to be part of the class definition, giving direct access to a private member variable undeniably violates encapsulation:
Is there any legitimate reason for doing something like this?Code:class { private: mutable int hidden; public: int& unHide () const { return hidden; } } thing ; int main() { // thing.hidden = 1; /* illegal */ int& hidden = thing.unHide () ; hidden = 1; return 0; }



LinkBack URL
About LinkBacks




CornedBee