menu

hjk41的日志

Avatar

Effective C++ 30. Avoid member functions that return non-const references or pointers less accessibl

The reason Stafford and Oregon, the author of Effective C++, gave for this is as follows:

The reason for making a member private or protected is to limit access to it, right? Your overworked, underpaid C++ compilers go to lots of trouble to make sure that your access restrictions aren't circumvented, right? So it doesn't make a lot of sense for you to write functions that give random clients the ability to freely access restricted members, now, does it? If you think it does make sense, please reread this paragraph over and over until you agree that it doesn't.


Quite humorous, right?
It is often a bad idea to return references or pointers to class members. But sometimes we have to do it for performance constraints. In that case, const references or pointers would be a good choice.

评论已关闭