Archived

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

GekkoCube

Overloading the = operator!!??

Recommended Posts

I have a class called CTest with an interger variable. When I instantiate an object of type CTest like this... CTest t1(5,10); // x=5, y=10. // or CTest t1; // defaults to x=0, y=0. ...then my operator= function works. However, instantiating with pointers like this... CTest *t1(5,10); // or CTest *t1; ...will eventually result in an error when I try to delete both pointers. delete t1; delete t2; If I delete *t1 OR *t2, then I dont get errors. But that means one or the other object has not been properly deleted! But if I delete BOTH, then my program crashes! So these problems basically occur when I create and delete objects with the new and delete keywords. Any insight/explanation is greatly appreciated. ~ I''''m a wannabe programmer ~

Share this post


Link to post
Share on other sites
How exactly are you doing the pointer version of CTest? The following should work:

  
CTest *t1 = new CTest(5,10); // allocate memory of size CTest

// for t1

CTest *t2 = new CTest; // do same for t2


*t1 = *t2; // copy contents of t2 to t1


delete t1; // free up t1

delete t2; // free up t2



Share this post


Link to post
Share on other sites
The problem was when I called the assigment.
I did this t2 = t2;
but it should''ve been *t2 = *t1; // assuming i used new to create them.

After some experimentation, I found that I could do this...

CTest *t1 = new CTest(5,10); // create a new object.
CTest *t2; // DO NOT create new object.

*t2 = *t1; // this will work!!

My question is this:
t2 = t1 doesnt work, but *t2 = *t1 does.
Why does this work and the other doesnt?
Does it mean im dereferencing the object???



~ I''''m a wannabe programmer ~

Share this post


Link to post
Share on other sites
when you say t1 = t2 remember that t1 and t2 are pointers so they have some freaxy hex value as their value.

so all you are doing is saying :

t1 points to wherever t2 points to.

thus t1 and t2 point to the same object so for the rest of your checking the values that t1 and t2 contain will appear to be equal because they are EXACTLY the same object.

you get an error on the deletes b/c when you say>

delete t1;

you free the memory held by the object that t1 points to.


but now you say:

delete t2;

well remember that t2 was pointing to the same object as t1, but now you have freed that memory. thus, t2 is pointing to a memory address that doesn;t contain any data. so you get an error.

i''d suggest some basic reading up on pointers. it''ll clear this all up for you fast.

-me

Share this post


Link to post
Share on other sites
BTW the expression:

*t1 = *t2;

works because you are saying make the object pointed to by t1 equal the object pointed to by t2. but in this case t1 and t2 continue pointing to DIFFERENT objects

hope that helps.

-me

Share this post


Link to post
Share on other sites
*t2 = *t1 actually means the t2 will point to the memory address that t1 is pointing to. so basically t2 is a copy of t1.

t2 = t1 (without an overloaded ''='' operator) means t2 takes the address of t1, and since t2 isn''t a pointer, you get an error. A class object works differently from a regular variable like int or double. Without an overloaded = operator, you just copy the memory address of the class

If you properly overloaded the = operator and copy the members of t1 into t2, it should work properly.

I may be mistaken with your question, but how does:

CTest t1(5,10); // x=5, y=10.
// or
CTest t1; // defaults to x=0, y=0.

means your operator= function works?

Share this post


Link to post
Share on other sites