Operator overloading ??

Hi :-)
I am just trying to figure out what the practical use of operator overloading is.... I came across a description of the syntax for operator overloading, and I can't seem to get get my head around it. My question is: What is this piece of code trying to achieve ? What is the point here, and exactly what does (i + rv.i) and (i+= rv.i) represent ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// OperatorOverloadingSyntax.cpp
#include <iostream>
using namespace std;

class Integer {
int i;
public:
Integer(int ii): i(ii) {}
const Integer operator+(const Integer& rv) const{
cout << "operator+" << endl;
return Integer(i + rv.i);
}
Integer& operator+=(const Integer& rv) {
cout << "operator+=" << endl;
i += rv.i;
return *this;
  }
};

int main() {
cout << "built- in types:" << endl;
int i=1, j=2, k=3;
k+= i+j;
cout << "user defined types:" << endl;
Integer ii(1), jj(2) , kk(3);
kk += ii + jj;
}


So far learning c++ I have had very few problems with syntax, but I don't get this...
That doesn't look like a very good example... Who ever wrote it didn't think of simplifying it for beginners, especially with that Integer class. I can hardly tell what's going on in there! :P

Operator overloading is when an operator has more than one function or use. For example, the plus (+) operator has multiple uses, depending on what you're doing with it.

In the statement: "int num = 2 + 2;" + is being used for addition.

In the statement: "string name = "John" + " " + "Doe"; + is being used to merge or concatenate the three strings into one. ("John Doe")

i += 2; is the equivalent to i = i + 2; (Or basically increasing i by 2.)
Operator overloading's purpose is for getting algorithms to work equally on classes and ordinary data types.
Consider an algorithm like this:
1
2
3
4
5
6
//takes the factorial of an integer-like thing
template <class T> T factorial (T a){
        T b,c = 1;
        for(b=2;b<=a;b++)c*=b;
        return c;
}

In order for this to work on some class like Bignum, the statements like c*=b must be defined for objects of class Bignum. That is the purpose of operator overloading. And, ultimately, that is the purpose of references too.
Last edited on
Oh, guess I thought "Operator overloading" meant "an Overloaded Operator," like Overloaded Functions. (No wonder I didn't understand the example code! :P )

Thanks for correcting me R.B.9000.
Last edited on
Thank you guys for feedback :-) I agree it is not a good example. I understand the purpose of operator overloading as you describe it and the examples you are using, but I have to figure out this perticular piece of code that I provided does.
I understand it like this: Usually, for a client programmer, the operator overloading is not usually neccessary, at least if it doesn't make your code easier to read or write. This piece of code is supposed to show how to make functions for the overloaded operator in classes or libraries for client - programmer use.
In main() , the first call to the operator+= works like you would expect for the operator "+=" in general, but when class objects Integer ii, jj, kk are declared and then then the overloaded "+=" is called for the class object "kk" , I get lost.......
Operator overloading is the same as function overloading

this

a = b + c

is the same as this

a = b.operator+(c)

just a function with a funny name. The compiler translates the nicer looking b + c to that function.

so its just about appearance, that's why its sometimes called "syntactic sugar"
I wouldn't do it like that example you posted. Every time I've used op overloading, I keep it simple, lol.

Here's one for outputting an object:
1
2
3
4
5
6
7
std::ostream &operator << ( std::ostream &stream, test &t )
{
	stream << "char: " << t.getChar() << "   "
			<< "int: " << t.getInt();

	return stream;
}


Now with this, instead of having to call 'test.getFoo()' functions etc within a class, I can simply do:
1
2
3
    testClass test;

    std::cout << test;
Last edited on
Thanks, guys :-)
Really appreciate the feedback :-)
Ok.
If I am not mistaken, the code I provided works like this.
In main(), the built- in types do not work with operator overloading, so the assignment simply translates from k+= i+j; , to k= k+(i+j).
Then , with the user - defined types, there is a call to the constructor for each of the objects
ii(1), jj(2) , kk(3) of class Integer, and the private member is initialized for each of them.
When the compiler encounters the expression kk+= ii + jj; , the operator+ -function is called for the object "ii". It takes a reference to "jj"(right hand side operand) as an argument and returns a temporary object of type Integer(the class).
Then the operator+= - function is called for the object "kk", and it takes the temporary object(rv) as an argument( the right hand side operand). It then returns the value at the address of the Integer class object "kk" by returning a dereferenced this - pointer( *this )

Please arrest me if I am mistaken, because I am hellbent on learning this thoroughly :-)
And thanks again to all contributers! I really do appreciate it:-)
Topic archived. No new replies allowed.