CIS 22 - Data Structure: Friend Operators vs. Member Operators
Most operators can be defined either as member functions or as
ordinary functions. (Some operators, such as assignment, must be
defined as member functions.)
What difference does it make?
-
A member function is automatically allowed to access the private parts of a
class.
Ordinary function are not normally allowed access to the private parts
of a class. If a non-member function requires access to the private part
of a class, the class must declare the function to be a friend.
-
If implicit type conversions are required,
only the right operand will be converted if the operator is defined as a member
function. If the operator is defined as an ordinary function, both
operands can be implicitly converted.
Source Code for Class String
- With member operator functions
- With friend operator functions
Back to CIS 22 home page