Hi,
I understand that operator loading is used to support user-defined types.
One of the requirements is that one of the arguments in an operator
overloaded function must be of a user defined type. My question is, how do
you know if this argument refers to the left or right operand in an
expression. I assume in a single argument it refers to the right operand.
Any help appreciated,
Regards
A
consider:
When overloading +, you can either declare it as a member function of its
class or as a friend function. For example:
class Date
{
public:
Date operator +(const Date& other); //member function
};
class Year
{
friend Year operator+ (const Year y1, const Year y2); //friend
};
Year operator+ (const Year y1, const Year y2);
The friend version is preferred because it reflects symmetry between the two
operands. Since built-in + does not modify any of its operands, the
parameters of the overloaded + are declared const. Finally, overloaded +
should return the result of its operation by value, not by reference.
Question: why not use a member function that takes two arguments like the
friend function so that it it too has the property of symmetry?
REgards
A