Overloaded Operators as Class Members

As I mentioned in the previous chapter, any function related to any programmer-defined data type can be implemented either as a class member function or as a global stand-alone nonmember function. This is true of any algorithm, and this is true of overloaded operator functions. Switching from class member to nonmember implementation and back is an important programming skill. It is especially important for operator functions.

The operator can be defined as a member function of the class of its parameters. The number of parameters is one less than the arity of the operator (one for binary, none for unary). This absent parameter becomes the target of the message when the operator is used.

Replacing a Global ...

Get Core C++ A Software Engineering Approach 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.