I'm learning C++ Primer 4th Edition now. This book suggests that we should define the arithmetic and relational operators as nonmember functions. Why?
I'm learning C++ Primer 4th Edition now. This book suggests that we should define the arithmetic and relational operators as nonmember functions. Why?
my guess would be so that you can make it communitive when mixing types for example
MyType answer = MyType + 5; //operator+ can be define as member or non-member
MyType answer = 5 + MyTyper; // can only be non-member
One should also prefer non-member non-friend functions if possible:
http://www.gotw.ca/gotw/084.htm