![]() |
|
#1
|
|||
|
|||
Operator overloading problemDear all,
I cannot quite understand the logic behind the notion of operator overloading. I understand that creating a class, is like creating a new type, therefore we can overload some operators to work with that new type. For example: CPP / C++ / C Code:
Consider the Ratio class as the following: CPP / C++ / C Code:
Here is the understanding problem: CPP / C++ / C Code:
Is this the only way to overload that operator in this example? How one has to think in order to writew the above code? What are the steps involved in order to write the above two lines of code? And secondly in the first example (operator=) the return type is a reference to the Ratio but in the second is not a reference. Why is not a reference. When should we use a reference and when not? Please bare in mind that I understand the use of friend functions. Thanks in advance for the help, grscot Last edited by LuciWiz : 21-Oct-2004 at 00:32.
Reason: Please insert your C code between [c] [/c] tags
|
|||
|
#2
|
||||
|
||||
|
First things first. Please refer to this page for some information on posting C++ code in the forum. Ok, now for the operators...
From what I can see, the only operator that hasn't been overloaded with a reference type for the return value is the implementation of the * operator. This is most likely a typo or a simple mistake. The code that you posted above, though more carefully organized, should look like this... CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
So, to answer your question, it should be quite apparent that a reference is used when you are going to change the value of a variable that is getting passed to the function or when you need the address of a return value to be passed on to another operator, such as the = operator or the [] operator. Hope this helps! __________________
-Aaron |
|
#3
|
||||
|
||||
|
Oh, just a quick note: you WILL want to overload the = operator for aggregate copying if your class contains dynamically allocated data. If it doesn't, then it's ok to just use C's convention for copying a class into another class of the same type. However, when you try to copy the contents of a class with dynamically allocated data into another class of the same type, bad things happen. You'll basically have what we call a memory leak. All of the pointers in one class are set to point to memory already pointed to by the class that you copied into it. Therefore, anything that was being pointed to by the class that was copied into is now floating around in limbo, and you can't get it back. The only way to fix a memory leak is to restart your computer, or to be using Unix, where memory leaks are not in the programmer's vocabulary. Anyhow, you probably won't run into this problem until later. If and when you do, just remember to deallocate all dynamic data that your pointers are pointing to BEFORE you alter the pointers.
__________________
-Aaron |
Recent GIDBlog
R for statistics by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Tee chart problem | Arun | C++ Forum | 0 | 01-Sep-2004 23:23 |
| C I/O problem | kelly80 | C Programming Language | 4 | 27-Apr-2004 20:15 |
| Another FX 5600 problem (but with details that might shed light on this) | BobDaDuck | Computer Hardware Forum | 2 | 16-Apr-2004 07:53 |
| problem with php5 cgi installation | fab13 | Apache Web Server Forum | 3 | 19-Nov-2003 09:11 |
| unwanted scrollbar problem | kelly001 | Web Design Forum | 3 | 24-Oct-2003 10:44 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The