Sign in to follow this  

copy constructor vs overloaded opeator=()?

This topic is 2538 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

Quote:
Original post by nuclear123
so what is the difference between a copy constructor(below)
Date::Date(const Date& olddate)
{
}

and this overloaded = operator?

Date Date::operator=(const Date& olddate)
{
}
Date a;
Data b = a; // copy constructor
a = b; // assignment operator

Share this post


Link to post
Share on other sites
Quote:
Original post by nuclear123
so what is the difference between a copy constructor(below)
Date::Date(const Date& olddate)
{
}

and this overloaded = operator?

Date Date::operator=(const Date& olddate)
{
}
The assignment operator is responsible for changing the objects old contents into something which equates to being the same as that of another object. Typically this means that it involves destroying or freeing its current resources and acquiring new resources, not necessarily in that order.
An example would be a string class which must free its existing buffer and allocate room for what may be a larger string, and copy the new contents across.

A copy-constructor on the other hand has no such pre-existing state to deal with.

Share this post


Link to post
Share on other sites
When the object is trivial to perform a deep copy, you can reuse the assignment operator code in the copy constructor.


Object::Object(const Object& object) {
*this = object;
}

const Object& Object::operator=(const Object& rhs) {
// do the deep copy.
}



However, in some cases the copy constructor would have to differ from the assignment operator.

Share this post


Link to post
Share on other sites
Quote:
Original post by c_olin
When the object is trivial to perform a deep copy, you can reuse the assignment operator code in the copy constructor.


Bad idea, you may be invoking undefined behaviour by operating on an unconstructed object. You will also likely not be able to provide the strong exception guarantee.

Unless you have darn good reason to do otherwise, you should implement your operator= in terms of the copy constructor and a non-throwing swap.

Date& Date::operator=(const Date& date)
{
Date tmp(date); // may throw an exception, ok, no harm done
swap(tmp); // may never throw
return *this;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by c_olin
When the object is trivial to perform a deep copy, you can reuse the assignment operator code in the copy constructor.

*** Source Snippet Removed ***

However, in some cases the copy constructor would have to differ from the assignment operator.
Bad idea indeed. You're saying that one should reuse something that does A and B inside something where you only want to do B.

You simply have it back to front. One may use the copy-constructor inside the assignment operator. In fact that's a good way to implement the well-known and exception-safe "copy and swap" idiom.
As a rule, you should never do it the way you spoke of. It only tends to work when either they are both trivial and need not be implemented at all, or when you do even more extra work to nullify the effects of A. That extra work can come from the initialisation list not being used, causing unnecessary work default initialising stuff.

Share this post


Link to post
Share on other sites
The assignment operator assigns. That is, it takes two already existing objects, and causes one to be equal to the other. The copy constructor constructs a copy. That is, it takes one already existing object, and causes the existence of a new object that is equal to the first one.

As for implementation, listen to Bregma and iMalc.

Although AFAIK, it is safe/correct and simpler to just pass by value to the assignment operator (which implicitly copies) rather than explicitly making a copy inside the function.

Share this post


Link to post
Share on other sites

This topic is 2538 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this