Page 1 of 1

Problem with Overloading the Assignment Operator

Posted: Tue Apr 06, 2010 9:37 pm
by Maevik
I am having trouble getting an overloaded assignment operator to work properly. Basically, after the assignment function is called, the values of the variable to which I am assigning remain unchanged.

Code: Select all

bullets[i] = bullets[count];  //bullets[i] remains unchanged
The class I am overloading is derived from another class which also has an overloaded =.

Code: Select all

Bullet& Bullet::operator=( const Bullet& p_bullet )
{
	if( this != &p_bullet )
	{
                // this ALSO REMAINS UNCHANGED AFTER THIS CALL TO =
		(Object)*this = (Object)p_bullet;                                 

		double tempDir = ( p_bullet.getDirection() );

		SDL_Rect tempRect;
		tempRect.x = this->getLoc()->x;
		tempRect.y = this->getLoc()->y;

		this->define( p_bullet.ownedBy , tempRect , tempDir , p_bullet.m_type );
	}
	return *this;
}
The code for the base class' overloading

Code: Select all

Object& Object::operator=( const Object& p_newObject )
{
	if( this != &p_newObject )
	{
		size = p_newObject.size;
		collisionRect = p_newObject.collisionRect;

		midpoint.x = p_newObject.midpoint.x;
		midpoint.y = p_newObject.midpoint.y;
                // AT THIS POINT THE VALUES ARE SUCCESSFULLY CHANGED, BUT THIS DOES NOT CARRY THROUGH AFTER THE RETURN

		clip.x = p_newObject.clip.x;
		clip.y = p_newObject.clip.y;
		clip.w = p_newObject.clip.w;
		clip.h = p_newObject.clip.h;
		imageName = p_newObject.imageName;
		usingClip = p_newObject.usingClip;
		active = p_newObject.active;
		sizeCoefficient = p_newObject.sizeCoefficient;
	}

	return *this;
}
The output from my debug log gives the following output:

Code: Select all

Bullet 0 out of bounds at 1335 , 619. Replacing with Bullet 4 at 711 , 619
Old Object: 711 , 619
New Object: 711 , 619
New Bullet: 1335 , 619
After = Call: 1335 , 619
This is basically telling me the coordinate values of the object (recieving assignment) before the assignment, then at the end of the Object = function, then after the Object = function returns to where it was called in the Bullet = function. Lastly the values after the Bullet = function call returns.

So I know that my Object::operator=() function is broken, and it's unclear from the data I have whether my Bullet::operator=() works. Any help?

Re: Problem with Overloading the Assignment Operator

Posted: Wed Apr 07, 2010 2:30 am
by K-Bal
I'm not seeing anything obvious. Seems like a perfect debugger use case ;)

Code: Select all

if( this != &p_bullet )
This is unneccessary, shit in, shit out.

Code: Select all

(Object)*this = (Object)p_bullet;
Try

Code: Select all

Object::operator=(p_bullet);
if you are not sure that above works.


And remove the p_, my eyes hurt :)

Good luck,
Marius

Re: Problem with Overloading the Assignment Operator

Posted: Wed Apr 07, 2010 2:56 am
by Maevik
Thanks! This got it working. Which is great cause now I can at least move on while I try to figure out what was wrong with it.