What is the difference between overloading operator= and overloading the copy constructor?
What is the d开发者_StackOverflowifference between overloading the operator =
in a class and the copy constructor?
In which context is each one called?
I mean, if I have the following:
Person *p1 = new Person("Oscar", "Mederos");
Person *p2 = p1;
Which one is used? And then when the other one is used?
Edit:
Just to clarify a little bit:I already know that if we explicitly call the copy constructor Person p1(p2)
, the copy constructor will be used. What I wanted to know is when each one is used, but using the =
operator instead, as @Martin pointed.
In your case neither is used as you are copying a pointer.
Person p1("Oscar", "Mdderos");
Person extra;
The copy constructor
Person P2(p1); // A copy is made using the copy constructor
Person P3 = p2; // Another form of copy construction.
// P3 is being-initialized and it uses copy construction here
// NOT the assignment operator
An assignment:
extra = P2; // An already existing object (extra)
// is assigned to.
It is worth mentioning that that the assignment operator can be written in terms of the copy constructor using the Copy and Swap
idium:
class Person
{
Person(std::string const& f, std::string const& s);
Person(Person const& copy);
// Note: Do not use reference here.
// Thus getting you an implicit copy (using the copy constructor)
// and thus you just need to the swap
Person& operator=(Person copy)
{
copy.swap(*this);
return *this;
}
void swap(Person& other) throws()
{
// Swap members of other and *this;
}
};
The copy constructor is a constructor, it creates an object. In particular, the copy constructor creates an object which is semantically identical to another, already existing object, of which it makes a "copy":
Person newperson(oldperson); // newperson is a "copy" of oldperson
The assignment operator isn't a constructor at all, but an ordinary member funcion that can only be invoked on an existing object. Its purpose is to assign to your object the semantics of another object, so that after the assignment the two are semantically identical. You are not usually "overloading" the assignment operator, you are just defining it.
Person p; // construct new person
/* dum-dee-doo */
p = otherperson; // assign to p the meaning of otherperson, overwriting everything it was before
// invokes p.operator=(otherperson)
Note that if it makes sense to compare to objects (with ==
), then both copy construction and assignment should behave so that we have equality afterwards:
Person p1(p2);
assert(p1 == p2);
p1 = p3;
assert(p1 == p3);
You are not forced to guarantee this, but users of your class will usually assume this behaviour. In fact, the compiler assumes that Person p1; Person p2(p1);
entails p1 == p2;
.
Lastly, as a final aside and as said elsewhere, note that Person p = p2;
literally means Person p(p2)
(copy construction), and never Person p; p = p2;
. That's syntactic sugar to allow you to write naturally-looking code without compromising efficiency (or even correctness, as your class may not even be default-constructible).
A copy constructor constructs a new object by using the content of the argument object. An overloaded assignment operator assigns the contents of an existing object to another existing object of the same class.
精彩评论