Choosing Member or Nonmember Implementation
When we define an overloaded operator, we must decide whether to make the operator a class member or an ordinary nonmember function. In some cases, there is no choice—some operators are required to be members; in other cases, we may not be able to define the operator appropriately if it is a member.
The following guidelines can be of help in deciding whether to make an operator a member or an ordinary nonmember function:
• The assignment (=
), subscript ([]
), call (()
), and member access arrow (->
) operators must be defined as members.
• The compound-assignment operators ordinarily ought to be members. However, unlike assignment, they are not required to be members.
• Operators that change the state ...
Get C++ Primer, Fifth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.