| Author |
Message |
guitarman fourth grade
Joined: 04 Nov 2005 Posts: 728
|
Posted: Wed Nov 23, 2005 1:22 pm Post subject: What is Operator Overloading (C++) |
|
|
Hello there,
This question has been raised at http://www.cambodiaxp.com/forum/why-some-people-prefer-c--vt24.html .
Operator Overloading in someway is similar to method overloading. We know that C++ as well as other programming language has built in (predefined) operators such as "+","-","%" etc.
For the sake of this discussion, let's take a look at an example of "+", plus operator. This operator will add integers or float numbers. However, let say we want to add a complex number (complex number A and Complex Number B). In this case, C++ won't be able to give us result by simply calling A + B, it simply won't work. To get this to work i.e. adding of complex number, you can perform in two ways. First is to create a custom method (function), for example ADD_COMPLEX. If you choose to use this method, it will look something like this:
Result = ADD_COMPLEX(A,B);
Definitely, it will look unpleasant. Isn't it better to just call it
Result = A+B; //Result will be a complex number as well
Therefore, Operator Overloading simply mean to "add another meaning" to the existing meaning depending on the "Class of the operands".
|
|
| Back to top |
|
 |
Singachea fourth grade
Joined: 19 Nov 2005 Posts: 258
|
Posted: Wed Nov 23, 2005 4:56 pm Post subject: |
|
|
Hmm...
I still don't get what you mean by overloading. As you say operator overloading is something similar to method overloading, and as I experienced from Java programming structure, method overloadings are the methods which have the same name with different signatures.
As what you mentioned, it's something like we casting only.
Can you provide some more tips about it because i'm blur now. 
|
|
| Back to top |
|
 |
guitarman fourth grade
Joined: 04 Nov 2005 Posts: 728
|
Posted: Wed Nov 23, 2005 9:09 pm Post subject: |
|
|
It overwrite the default "meaning" or you can call it "method" depending on the type of the operands it operates on. Whereas casting is basically change the data type of the operands. So the different here is that the operator overloading will changes its meaning according to the operands and type casting will change the type of the variable(copy of variable) itself not the operands.
As to specific example, you may refer to books coz frankly, i didn't experience in programming these languages but roughly understand the concepts. 
|
|
| Back to top |
|
 |
|
|