Problem with Overloading the Assignment Operator

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Maevik
Chaos Rift Junior
Chaos Rift Junior
Posts: 230
Joined: Mon Mar 02, 2009 3:22 pm
Current Project: www.keedepictions.com/Pewpew/
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: Long Beach, CA

Problem with Overloading the Assignment Operator

Post 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?
My love is like a Haddoken, it's downright fierce!
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: Problem with Overloading the Assignment Operator

Post 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
User avatar
Maevik
Chaos Rift Junior
Chaos Rift Junior
Posts: 230
Joined: Mon Mar 02, 2009 3:22 pm
Current Project: www.keedepictions.com/Pewpew/
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: Long Beach, CA

Re: Problem with Overloading the Assignment Operator

Post 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.
My love is like a Haddoken, it's downright fierce!
Post Reply