• Advertisement

Archived

This topic is now archived and is closed to further replies.

Overloaded operator= doesn't work, program still calls the "Default" one

This topic is 5031 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

class KBall
{
 public:
 KBall(): Bomb(0) {};
 float X;         //position of the ball

 float Y;         //position of the ball

 float VX;			//vector X (of movement)

 float VY;			//vector Y (of movement)

 float Speed;     //ball's speed

 float CurveSpeed;//how much the ball curves towards a pad

 bool Bomb;			//is the ball a bomb?

 bool LastPad;		//last pad hit

 const KBall &operator=(const KBall &);
 KSprite sprite;  //sprite of the ball

 void Reset();    //resets the ball

 void Draw();		//draws the ball, w00t w00t hey bannana man ownz j00


};
//=======================================================

const KBall &KBall::operator=(const KBall &Ball)
{
 X = Ball.X;
 Y =  Ball.Y;
 VX = Ball.VX;
 VY = Ball.VX;;
 Speed = Ball.Speed;
 CurveSpeed = Ball.CurveSpeed;
 Bomb = Ball.Bomb;
 LastPad = Ball.LastPad;

 return *this;
}

My custom class KSprite has some pointers in it. Therefore, I cannot use the default = operator to copy a KBall variable. So I overloaded it (skipping KSprite as I don't really need it in a copy) and when I do this:
KBall New_Ball = *Old_Ball;
 
( the '*' is added because I pass the old ball as a pointer to my function that copies the passed-in ball) It uses the "default" method and skips my function entirely! Of course, it will work if I do this:
KBall New_Ball;
New_Ball.operator=(*Old_Ball);
 
But a simple = looks so much nicer... I really don't know why it doesn't work, I checked with my book and everything seems to be fine... Also on another note, like I said my KSprite class has some pointers to another of my custom class which ALSO has some pointers (to in this time). Something like this:
class Foo
{
 private:
 int * Blah;
};

class MegaFoo
{
 private:
 Foo * Yargh;
};
what's a good way of solving this problem if I want to copy a MegaFoo variable? I would have to go deep into Foo and then to Blah and copy it, but you see, I don't want the coder to give access to Foo and Blah through MegaFoo. So he can't do this:
MegaFoo A;
A->Yargh->Blah = B->Yargh->Blah;
 
So how can I make it easy for the user to copy these variables without giving him access to it? Should I simply overload an operator= function in my MegaFoo, which would basically do the internal copying itself (I think it's the best choice cause then the user can simply type MegaFoo_1 = MegaFoo_2 and don't worry about it)? Or should I toy with constructors/deconstructors and do all the work when he calls the generic = operator? Thanks for replys!!!! Someday, maybe not tomorrow, maybe not next week, but some day, you'll all get cookies for your help! [edited by - Koobazaur on April 13, 2004 8:47:21 PM]

Share this post


Link to post
Share on other sites
Advertisement
quote:
Original post by Koobazaur

My custom class KSprite has some pointers in it. Therefore, I cannot use the default = operator to copy a KBall variable. So I overloaded it (skipping KSprite as I don''t really need it in a copy) and when I do this:

KBall New_Ball = *Old_Ball;



Welcome to the wonderful world of C++ weirdness. That line doesn''t call operator=(). It calls the copy constructor. Implement a copy constructor that does what you want operator=() to do, and all should be well.

Share this post


Link to post
Share on other sites
I don''t know what the problem is with the first on is, but in answer to your second question, you should overload the copy constructor and/or = operator in both classes, and the MegaFoo will copy Foo in the same way the user copies MegaFoo, with the = operator or copy constructor. On second thought, the copy constructor may have something to do with your first problem too.

tj963

Share this post


Link to post
Share on other sites
try this


KBall::KBall ( const KBall & T ){
this->operator= ( T );
}

//NOTE: this above code will work if the object is not constant
if so then you should assign all variables when they are defines (what this means is)
you should assign values like

KBall::KBall ( const KBall & T )
:Bomb(0) , X(something1), Y (...... //all variables
{
/*you don''t need to do any thing here except some other things to do */
}



i hope this helps

Share this post


Link to post
Share on other sites
Hmm, ok, I'll fiddle with the copy constructor.
So what's the point of overloading operator= when it always calls the copy constructor (unless there is a way to make it work)


[edited by - Koobazaur on April 14, 2004 9:03:10 PM]

Share this post


Link to post
Share on other sites
The copy constructor is called when the variable is initialized on the same line it is defined. This:

MyClass foo = 3;

is different than:

MyClass foo;
foo = 3;




"Sneftel is correct, if rather vulgar." --Flarelocke

Share this post


Link to post
Share on other sites
It''s an optimization. C and C++ stress optimal performance over code structure, and it''s faster to set things in the constructor than to call the default constructor and then call the function that sets things. So the copy constructor uses one function call, whereas the other way uses two.

The constructor can be explicitly called like this:

KBall New_Ball( *Old_Ball );

But this does the same thing, and looks better:

KBall New_Ball = *Old_Ball;

~CGameProgrammer( );

Screenshots of your games or desktop captures -- Upload up to four 1600x1200 screenshots of your projects, registration optional. View all existing ones in the archives..

Share this post


Link to post
Share on other sites

  • Advertisement