Reputation: 71
Could somebody explain me the difference between copying and assignment?
SomeClass a;
SomeClass b = a; // assignment
SomeClass c(a); // assignment
b = c; // copying
but what is the difference, why there are two different constructions in the language?
Upvotes: 6
Views: 185
Reputation: 20616
Copying is for initializing new objects by copying the contents of existing ones, assignment is for overwriting existing objects with the contents of other objects - the two are very different things. In particular, this
SomeClass a;
SomeClass b = a;
is copy initialization - you're copying a
to create a new SomeClass called b
using syntax of the form
T x = y;
This has the effect of invoking SomeClass
's copy constructor (assuming there is one and it's accessible). The compiler-generated default copy constructor would do a memberwise copy of a
; you can replace it with your own as needed, e.g.
SomeClass(const SomeClass& rhs)
: x(rhs.x)
{}
(Note that this is a very boring example, as it just does what the default memberwise copy constructor might.)
Moving on, this
SomeClass c(a);
is direct initialization using the copy constructor. It will generally have the same effect as the above, but this is worth a read:
http://www.gotw.ca/gotw/036.htm
Also, see here:
http://www.gotw.ca/gotw/001.htm
Your final case, namely
b = c;
is assignment. The semantics of this should generally be to overwrite b
with the contents of c
(although some things, such as std::auto_ptr
, have strange assignment semantics, so watch out). To implement your own assignment operator, you write something like this (note that this is a very boring example, as it just does what the default memberwise assignment operator might):
SomeClass& operator=(const SomeClass& rhs)
{
x = rhs.x;
return *this;
}
In practice, however, you have to be careful about exception safety in situations like this, which leads to things like the popular copy-and-swap idiom for implementing assignment operators. See here:
http://en.wikibooks.org/wiki/More_C++_Idioms/Copy-and-swap
Upvotes: 1
Reputation: 258568
Initialization only happens once, when the object is created. If by copying you mean calling the copy constructor, than copying is a form of initialization. Assignment can happen any number of times.
Now, on to your example, all of those are wrong:
SomeClass a();
This declares a method called a
which takes no parameters and returns an object SomeClass
.
SomeClass b = a; // actually copy constructor & initialization of b
SomeClass c(a); // same
If a
were a SomeClass
object, these two would be initialization, and it calls the copy constructor - SomeClass::SomeClass(const SomeClass&)
. They are equivalent.
b = c; // assignment
If c
is a SomeClass
object, this is assignment. It callse SomeClass::operator =(const SomeClass&)
.
Upvotes: 3
Reputation: 49802
Initializations initialize a previously uninitialized object. Assignments, on the other hand, overwrite an already initialized object and may have to destroy existing state. These are different operations; while they usually have the same outcome for the object on the LHS, they are not semantically equivalent.
Upvotes: 1
Reputation: 272467
This is initialization (but it calls the copy constructor):
SomeClass b = a;
So is this:
SomeClass c(a);
This is assignment:
b = c;
Oh, and this isn't an initialization:
SomeClass a();
Upvotes: 2