C++ will create copy constructor and copy assignment operator for class. My questions is why it is creating two member function ? What is the significance and what is the difference by copying the object by copy constructor and copy assignment operator? Thank you in advance.
-
3They don't perform the same task. One performs copy construction, the other performs assignment. – juanchopanza Feb 23 '14 at 14:02
-
C++ generates a whole set of so called Canonical Methods. You probably should explicitly define them all yourself. A copy ctor is unary operator, an assignment operator is a binary operator. In some cases they behave the same, in some cases they don't :) – Patrick Savalle Feb 23 '14 at 14:08
-
2@PatrickSavalle why define them yourself? If the do nothing on top of the defaults, it's better not to define them yourself, because it's not idiomatic. – Luchian Grigore Feb 23 '14 at 14:11
-
A constructor initialized an object. An assignment changes the state of an existing one. – juanchopanza Feb 23 '14 at 14:13
-
Even i can initialize object using assignment also... – kayle Feb 23 '14 at 14:15
-
possible duplicate of [What's the difference between assignment operator and copy constructor?](http://stackoverflow.com/questions/11706040/whats-the-difference-between-assignment-operator-and-copy-constructor) – Mat Feb 23 '14 at 14:16
-
Voting to reopen because the closing reason sucks. It's perfectly clear what OP is asking. – jrok Feb 23 '14 at 14:18
-
No, you cannot initialize an object using assignment. – juanchopanza Feb 23 '14 at 14:21
-
@jrok The original made no sense. It asked why something that is not true happens. – juanchopanza Feb 23 '14 at 14:22
-
@juanchopanza does `int x; x = 3;` have a different name? Is `x` not initialized after `x=3`? – Luchian Grigore Feb 23 '14 at 14:31
-
@LuchianGrigore `x` is already [*default-initialized*](http://en.cppreference.com/w/cpp/language/default_initialization) before the assignment. Assignment is not initialization. – juanchopanza Feb 23 '14 at 14:34
-
I dont know why it is put on hold... ? – kayle Feb 23 '14 at 15:01
2 Answers
Copy assignment and copy construction do different things. Copy assignment has to take a fully constructed object and change it, while copy construction has to take a non-fully constructed object and do that initial construction.
For example copy assignment on a class that manages a resource has to ensure that its old resource is properly disposed of after it has taken ownership of the 'copied' resource, whereas the copy constructor doesn't have any previous resource to deal with.

- 86,085
- 15
- 179
- 244
If you have pointers as data members in your class, and if they are directly getting copied to another object, more than one object will access that pointer memory (unintentionally). To avoid that we can override copy constructor/assignment operator.
If you do not override these two functions, compiler copies bit by bit to another object.

- 202,337
- 40
- 393
- 406

- 9,721
- 10
- 61
- 137