-
March 2nd, 2011, 04:57 PM
#1
operator= and destructor
If I have an object and try to assign a new object to it using operator=, does the original object's destructor get called before being assigned the new object?
If not, is there anyway to destroy the original object so that any memory allocated in the original object is not lost?
------------------------------------------------------
There are only 10 types of people in the world.
Those who understand binary and those that don't.
-
March 2nd, 2011, 05:06 PM
#2
Re: operator= and destructor
Originally Posted by mazeing
If I have an object and try to assign a new object to it using operator=, does the original object's destructor get called before being assigned the new object?
No.
If not, is there anyway to destroy the original object
Why? You are just to make the contents of the original object logically equal to the other object's contents. If that requires dynamic memory handling, then that's what you do.
Regards,
Paul McKenzie
-
March 2nd, 2011, 05:10 PM
#3
Re: operator= and destructor
It's possible to use a destructor to get rid of the old contents via the "copy and swap" idiom:
Code:
void MyClass::swap(MyClass &rhs)
{
std::swap(field1,rhs.field1);
std::swap(field2,rhs.field2);
...etc
}
MyClass& MyClass::operator=(const MyClass &rhs)
{
if (&rhs != this)
{
MyClass copy(rhs);
copy.swap(*this);
}
}
Note, the check for self-assignment isn't strictly necessary here----it's an optimization. If self-assignment is not going to happen often, you may actually lose more time on the if statement than on avoiding the copy in this case, so you may be better off without it!
-
March 2nd, 2011, 05:26 PM
#4
Re: operator= and destructor
Originally Posted by Lindley
It's possible to use a destructor to get rid of the old contents via the "copy and swap" idiom:
Code:
void MyClass::swap(MyClass &rhs)
{
std::swap(field1,rhs.field1);
std::swap(field2,rhs.field2);
...etc
}
MyClass& MyClass::operator=(const MyClass &rhs)
{
if (&rhs != this)
{
MyClass copy(rhs);
copy.swap(*this);
}
}
Note, the check for self-assignment isn't strictly necessary here----it's an optimization. If self-assignment is not going to happen often, you may actually lose more time on the if statement than on avoiding the copy in this case, so you may be better off without it!
My class is a two dimensional array with fields for height, width and the data (which is height*width elements dynamically allocated inside my class). If I use the "copy and swap" idiom, what happens if the size of the new array is different from the original?
Would this actually call my destructor to delete the original data member?
Code:
void Array2D::swap(Array2D &rhs)
{
std::swap(height,rhs.height);
std::swap(width,rhs.width);
std::swap(data,rhs.data);
}
Array2D& Array2D::operator=(const Array2D &rhs)
{
if (&rhs != this)
{
Array2D copy(rhs);
copy.swap(*this);
}
}
Last edited by mazeing; March 2nd, 2011 at 05:30 PM.
------------------------------------------------------
There are only 10 types of people in the world.
Those who understand binary and those that don't.
-
March 2nd, 2011, 05:33 PM
#5
Re: operator= and destructor
Assuming that you've correctly written your copy constructor and destructor, yes. This is simply a way of defining operator= in terms of those two things. The cctor is used to construct "copy", then you swap your copied data into this and the old this fields into copy, and then when "copy" goes out of scope its destructor cleans up the old fields previously belonging to this.
Note, however-----it may be desirable to special-case if the height and width of the rhs are the same as those of this, because in that case no memory handling is necessary.
-
March 2nd, 2011, 05:34 PM
#6
Re: operator= and destructor
Originally Posted by mazeing
My class is a two dimensional array with fields for height, width and the data (which is height*width elements dynamically allocated inside my class). If I use the "copy and swap" idiom, what happens if the size of the new array is different from the original?
Then the question is how did you implement this class? If you used std::vector<>, then you don't need to write any code, as vectors assigned to other vectors know how to resize themselves.
Also, your original question about destructors -- the original object isn't destroyed -- the contents within the object are changed and possibly destroyed, but the value of this doesn't change. Is this what you're asking?
Regards,
Paul McKenzie
-
March 2nd, 2011, 05:35 PM
#7
Re: operator= and destructor
Great!
Thanks.
------------------------------------------------------
There are only 10 types of people in the world.
Those who understand binary and those that don't.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|