Introduction to C++
Operator overloading with global functions
bool operator == (const time24 &lhs, const time24 &rhs)
return lhs.hour == rhs.hour && lhs.minute == rhs.minute; // this is a good
example of something we CAN do
//Assignment operators, e.g. t1 = t2; if not overloaded, default memberwise assignment
// but we CANNOT overload the assignment operator with a global function
//Consider assignment of ints
//1 = x ; the left-hand operand must be an object. So this would compile if overloading
with global functions were allowed.
// Instead, we must overload the assignment operator with a class member function.
Operator overloading with class member functions
lhs op rhs;
// returnType lhsClassName :: operator op ( type rhs )
bool time24:: operator == (const time24 &rhs)
return ( hour == rhs.hour) && (minute == rhs.minute); // we’re in the 24class, i.e.
self or this, so we can access hour and minute
time24 &time24 :: operator = (const time24 &rhs)
hour = rhs.hour;
minute = rhs.minute;
return *this // this is a pointer to the call