Item-23 Prefer non-member non-friend function to member function
· β 4 min read
Prefer non-member non-friend functions to member functions for better encapsulation, packaging flexibility, and functional extensibility.
Prefer non-member non-friend functions to member functions for better encapsulation, packaging flexibility, and functional extensibility.
Declaring data members private
gives clients syntactically uniform access to data, affords fine-grained access control, allows invariants to be enforced, and offers class authors implementation flexibility.
Never return a pointer or reference to a local stack object, a refenrence to a heap-allocated object, or a pointer or reference to a local static object if there is a change that more than one such object will be needed (item 4 provides a “counter” example that is reasonable in single-threaded environments).
Pass by reference-to-const
is typically more efficient than pass by value and avoids the slicing problem.