The main motivation is that by preferring non-member nonfriend functions, you can simplify classes as much as possible. See Herb Sutter's Article here: http://www.gotw.ca/publications/mill02.htm .
This article also provides an answer to another part of your question, where to put the appropriate get function. This is a C ++ function called argument dependent (ADL). From Herb Sutter, who calls it Koenig's search, although this name is controversial (see Comments below):
A Koenig search says that if you provide an argument to a class function, enter, then, to find the name of the function, the compiler must look not only in ordinary places, such as the local area, but also in the namespace (here NS), which contains the type of the argument.
Here is an example:
namespace MyNamespace { class MyClass {... }; void func(MyClass); } int main(int aArgc, char* aArgv[]) { MyNamespace::MyClass inst; func(inst);
So you just declare a get function in the same namespace as your class.
Remember that this does not work for template functions if you must explicitly specify template parameters - see this post: fooobar.com/questions/82370 / ...
source share