Jack Knudson
Jack Knudson

Reputation: 51

Copy constructor, assignment operator, and destructor code duplication

I understand the need for deep copies, and to ensure my code behaves as desired, I am writing a copy ctor, assignment operator, and dtor for my class.

However, it seems to me that in every case, an assignment operator has to do first what the destructor does (deallocate any dynamically allocated memory, to prevent memory leaks), and then what the copy constructor does (make deep copies of all dynamically allocated data and copy those into the instance being constructed).

Is there a case where an assignment operator should conceptually do something other than the following?

class SomeClass{
    //member data, copy ctor, dtor
    SomeClass& operator=(SomeClass const& rhs){
        //what the destructor does
        //what the copy constructor does
    }
};

The code is practically identical, and seems like a waste of time to rewrite. Other than invoking the destructor directly at the beginning of the assignment operator, I can't think of a way to reuse the copy constructor code I've already written, since I believe doing something like

*this=rhs

would only recursively invoke the assignment operator, since techinically, "this" has already been constructed.

Upvotes: 1

Views: 1362

Answers (1)

jxh
jxh

Reputation: 70382

As mentioned in the comments, your concerns about code duplication is addressed by applying the copy-and-swap idiom:

class SomeClass{
    //member data, copy ctor, dtor
    SomeClass& operator=(SomeClass rhs){
        swap(*this, rhs);
        return *this;
    }
    friend void swap(SomeClass& first, SomeClass& second) {
        using std::swap;     
        swap(first.data, second.data);
        //... etc. for other data members
    }
};

You have to implement the additional swap function, but your copy constructor and destructor code is reused in a natural way. The copy constructor is used when the source of the assignment is passed to the assignment operator, and the destructor is reused when the argument is destructed when the assignment operator returns.

Upvotes: 1

Related Questions