By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,305 Members | 1,614 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,305 IT Pros & Developers. It's quick & easy.

C++: operator overloading

P: n/a
A
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
Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hello A,

"A" <A@iprimus.com.au> writes:
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.


You may choose which argument is user-defined. If you write your own matrix
class and want to be able to add integers to it (perhaps it will add so
many times the unit matrix to your original matrix) you should define

matrix operator+(const matrix&,const int)

and, because hopefully your addition is commutative also

matrix operator+(const int,const matrix&)

The point is that these two + operators may make sense but that I would
really started hating a library writer that redefines the additon of two
integers. Fortunately the language forbids this (Although, you may turn
int into a macro that expands to my_int and then define an operator+ for
it, of course...)

Bye,
Chris Dams
Jul 19 '05 #2

P: n/a
A
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
Jul 19 '05 #3

P: n/a
On Tue, 11 Nov 2003 19:17:53 +1030, A <A@iprimus.com.au> wrote:
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.
No, it refers to the hidden this argument( left in the expression).
Implicit conversions can be aplied to the right argument.
In the friend version implicit conversions can be applied to match
both of the arguments of defined operator.
One of the arguments of the friend version must be of class/struct type,
that is you cannot overload operator for builtin types.

Here is an example:

struct intm {
int dat;
intm(int x) : dat(x) {}
intm operator +(const intm& y) { return intm(dat+y.dat); }
};
struct intf : intm {
intf(int x) : intm(x) {}
intf(const intm& m) : intm(m) {}
friend intf operator +(const intf& l,const intf& r) { return
intf(l.dat+r.dat); }
};

int main() {
intm mx=intm(5)+6;
// intm my=6+intm(5); will not work , member version
intf fx=intf(5)+6;
intf fy=6+intf(5);
intf fz=7+mx; // this works because of intf operator + , and implicit
conversions
}


Any help appreciated,
Regards
A


--
grzegorz
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.