While puzzling with some facts on class design, specifically whether the functions should be members or not, I looked into Effective c++ and found Item 23, namely, Prefer non-member non-friend functions to member functions. Reading that at first hand with the web browser example made some sense, however convenience functions( named the nonmember functions like this in the book) in that example change the state of the class, don't they?
So, first question, should not they be members then?
Reading a bit further, he considers the STL functions and indeed some functions which are not implemented by some classes are implemented in stl. Following the ideas of the book they evolve into some convenience functions that are packed into some reasonable namespaces such as
std::sort
,std::copy
fromalgorithm
. For instancevector
class does not have asort
function and one uses the stlsort
function so that is not a member of the vector class. But one could also stretch the same reasoning to some other functions in vector class such asassign
so that could also not be implemented as a member but as a convenience function. However that also changes the internal state of the object like sort on which it operated. So what is the rationale behind this subtle but important (I guess) issue.
If you have access to the book can you clarify these points a bit more for me?