GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 17-Feb-2004, 19:53
hyapici hyapici is offline
New Member
 
Join Date: Nov 2003
Posts: 3
hyapici is on a distinguished road

operator overloading


Hello 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:
	void operator= ( Point& other ){
		x = other.x;
		y = other.y;
	}

	void operator+= ( Point& other ){
		x += other.x;
		y += other.y;
	}

	void operator+ ( Point& other ){
		x = x + other.x;
		y = y + other.y;
	}
Last edited by JdS : 18-Feb-2004 at 04:55. Reason: use C++ syntax highlighter
  #2  
Old 18-Feb-2004, 05:25
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
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:
void operator+= ( Point& other ){
    x += other.x;
    y += other.y;
}
This will do a+=b nicely, but how about c=a+=b? Deconstructing the latter into c = a.operator+=(b), we see operator+= returns a void, so c is void.

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:
Point& Point::operator+=(Point& other){
	x += other.x;
	y+= other.y;
	return *this;
}

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:
Point operator+(const Point& lhs, const Point& rhs){
	Point result = lhs; //create a new Point object, let's not alter anything
	result += rhs; //we've defined the += operator already!
	return result;
}
Notice for this you'll need a copy constructor in the Point class.

Hope this helps
GF
Last edited by Garth Farley : 18-Feb-2004 at 05:26. Reason: Typos
  #3  
Old 18-Feb-2004, 19:37
hyapici hyapici is offline
New Member
 
Join Date: Nov 2003
Posts: 3
hyapici is on a distinguished road

that helped a lot!


Thanks Farley, you are my man! :-)
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:26.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.