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 ?
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.
Oh, guess I thought "Operator overloading" meant "an Overloaded Operator," like Overloaded Functions. (No wonder I didn't understand the example code! :P )
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.......
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:-)