CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2003
    Location
    Ridgecrest, CA
    Posts
    108

    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.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: operator= and destructor

    Quote Originally Posted by mazeing View Post
    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

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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!

  4. #4
    Join Date
    May 2003
    Location
    Ridgecrest, CA
    Posts
    108

    Re: operator= and destructor

    Quote Originally Posted by Lindley View Post
    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.

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: operator= and destructor

    Quote Originally Posted by mazeing View Post
    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

  7. #7
    Join Date
    May 2003
    Location
    Ridgecrest, CA
    Posts
    108

    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
  •  





Click Here to Expand Forum to Full Width

Featured