Problem with Overloading the Assignment Operator
Posted: Tue Apr 06, 2010 9:37 pm
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.
The class I am overloading is derived from another class which also has an overloaded =.
The code for the base class' overloading
The output from my debug log gives the following output:
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?
Code: Select all
bullets[i] = bullets[count]; //bullets[i] remains unchanged
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;
}
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;
}
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
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?