![]() |
|
#1
|
|||
|
|||
operator overloadingHello Everyone!
I have a problem with operator overloading. I wrote couple of methods to make operations with my class Point (double x, double y), and I call those methods from several different places in my code. The problem is: Once it is first called, somehow it stores the values in itself and whenever I want to make another operation with those overloaded operators it returns those stored values, i.e. does not assign the values where it is called. Is there anybody to help wit this problem? CPP / C++ / C Code:
Last edited by JdS : 18-Feb-2004 at 04:55.
Reason: use C++ syntax highlighter
|
|
#2
|
|||
|
|||
|
Hey!
Your problem is with your definitions of the overloaded operators, you're not using them properly. Let's start with the += operator. You've written: CPP / C++ / C Code:
This operator should return something, a reference to a Point, in this case to its newly altered self. Also, this function needs access to the x&y components of the Point class, so it should be a method of the Point class. So change this to: CPP / C++ / C Code:
Similarily for the operator=() function, it should be a member function and return *this. Lastly, is the operator+ function. Plus is a binary operator, it takes 2 arguments, the LHS and the RHS of the plus sign, and returns the answer. This doesn't need to be a member function of the Point class, if you do things right. So have a look: CPP / C++ / C Code:
Hope this helps GF Last edited by Garth Farley : 18-Feb-2004 at 05:26.
Reason: Typos
|
|
#3
|
|||
|
|||
that helped a lot!Thanks Farley, you are my man! :-)
|
Recent GIDBlog
Writing a book by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The