Skip to main content
gamedev.net
🔒 Locked

Operator Overloading?

Started by CloudNine Dec 22, 2002 at 4:22 AM 2 replies 200+ views
Original Post
CloudNine
CloudNine
Another question from me.... What''s the point in operator overloading? Thanks in advance, CloudNine
Russell
Russell
The point is you can write code that makes sense by using the operators. If you create your own string class, you can concatenate strings using the + operator. If you create an arbitrary sized integer class, you can write the +, -, *, /, etc. operators for it. If you didn''t do that, you would have to do something like:

BigInt a = 2134325423652364565324543252;
BigInt b = 23423456324566;
BigInt c = add(a,b);

Instead, since you can overload operators, you can do this:

BigInt c = a + b;

That is much more clear to a C/C++ programmer. You could also overload the input and output stream operators for your class. So you could do this:

cin >> a >> b;
c = a + b;

I don''t know of any case that they are necessary, but it''s cool to be able to use them with iostream stuff instead of calling a function like, display_big_int(BigInt & b), or whatever.
civguy
civguy
When doing vector math, you don''t have to do:

a.add(b).multiply(c)

You can do
(a+b)*c

And in cout you can do
cout << "blee" << x;

instead of
cout.print("blee").print(x);

etc
LilBudyWizer
LilBudyWizer
So you can code "a operator b" rather than "func(a,b)" or "a.func(b)". Or in more elaborate situation "a + b + c + d" rather than "a.sum(b.sum(c.sum(d)))" or "sum(a,sum(b,sum(c,d)))".
Keys to success: Ability, ambition and opportunity.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.