I have gone through [question] (What's the difference between assignment operator and copy constructor?) and understood difference between a copy constructor and assignment operator. Now my question is although copy constructor initializes the previously uninitialized object where as assignment operator replaces data from previously initialized object what's the difference in terms of final outcome. I am thinking that final outcome in both case comes out to be same right? In the end after copying via (CC) we get the same output and even after using (AO) we get the same output. Am I making sense here? Can someone please clarify what is the difference in terms of real world application?
-
@deviantfan : Suppose I have worked on one object and done some processing. Now **at the end** of my program I use copy constructor on one object and also use assignment operator on another and then use **print function** to see the values. Output will come out to be same right in both the cases right since both are kind of copying the same objects that I worked on earlier in my program?! – tanz Jan 06 '15 at 10:09
-
If you have a sane CC, yes. The important difference is, if you create a new object just to assign something in the next line, the normal constructor could unnecessarily waste time (and memory). Added a example below. – deviantfan Jan 06 '15 at 10:14
5 Answers
The final outcome depends entirely on the situation. You just need to understand that
Copy constructor is called when a new object is created from an existing object, as a copy of the existing object.
Assignment operator is called when both the objects are already created and we are assigning one to other. ex: a = b
Difference:
Copy constructor creates a new object which has the copy of the original object .
On the other hand assignment operators does not create any new object. It instead, deals with existing objects.
For pointer copying we need Deep copy as compiler's default copy constructor and assignment operator does Shallow copy . So Check this out for deeper understanding Shallow copy vs Deep copy

- 1,307
- 12
- 18
It depends on you. The language makes no requirements with
regards to what the copy constructor or assignment actually do.
Of course, any clients will expect that the observable state of
the object be the same as that of the copied object, in both
cases; this includes code in the standard library which may copy
or assign to your object types. But there are legitimate cases
where the class might contain meta information, used for
instrumentation or resource management, which could be different
in the two cases. (It's also frequent for a class to support
copy construction, for use in a clone
function, but not to
support assignment.)
Of course, the implementation may be very different; if the object contains a pointer to dynamically allocated memory, for example:
class Toto
{
Titi* myPtr;
public:
Toto() : myPtr( new Titi ) {}
Toto( Toto const& other ) : myPtr( other.myPtr->clone()) {}
~Toto() { delete myPtr; }
Toto& operator=( Toto const& other )
{
Titi* tmp = other.myPtr->clone();
delete myPtr;
myPtr = tmp;
return *this;
}
};
(In such a simplified case as this, of course, you'd not use
a pointer, and just have the class contain an instance of
Titi
. In a more complicated case, where Titi
is in fact an
abstract base class, and you instantiate different derived
classes according to some arguments to the constructors,
something like this may be justified, however.)

- 150,581
- 18
- 184
- 329
In copy constructor you are creating new object in assignment you are assigning value to the existing object from other object of same time. So the output may be different the way you implement copy constructor and assignment operator functions

- 335
- 1
- 3
- 14
Other than what you said, there is no difference.
The CC works on new unitizialied objects (it´s a constructor after all), the operator on existing ones.
The use of the CC could be replaced with the normal constructor and then the assignment op (in usual classes), but this would be not as efficient as the direct construction with copied data.
Eg.
class C
{
private:
vector<int> v;
public
C()
{
//fill v with 10^9 slowly generated random numbers, takes ca. 2 days
}
C(const C& c) //could be auto-generated in this case
{
v = c.v;
}
C &operator=(const C& c) //could be auto-generated in this case
{
v = c.v;
return *this;
}
};
...
C oldc;
...
//either
C newc(oldc);
//or
C newc; //takes 2 days
newc = oldc;
Another reason, some nontrivial classes have no (public) default constructor and can only be created by copying existing objects from somewhere.

- 11,268
- 3
- 32
- 49
-
So there will not be any difference in terms of coding also right?! Here you have given your own copy constructor. If you were to overload Assignment Operator also. The coding logic will be more or less same right?! – tanz Jan 06 '15 at 10:16
-
@tanz Woah, forgot to add it. But yes, in this case, there are no real differences. It could make a difference eg. when you´re allocating something with malloc/new in the constructor: The CC has to allocate too, the operator can rely that it is already there. Or if the constructor creates a thread or modifies some object counter or... – deviantfan Jan 06 '15 at 10:18
-
3@deviantfan In fact, there is no particular relationship between what the copy constructor does and what assignment does, _except_ the one that you as the programmer define. – James Kanze Jan 06 '15 at 10:32
-
-
2@deviantfan But you missed the important point: it's up to the author of the class to make the class behave in a sane manner; there's nothing in C++ which requires it. (Also, there are a few special cases where the internal state of an object will be different depending on whether it was copy constructed or assigned. Think of something like a use count: the copy constructor will set it to 0, regardless of its value in the source, and an assignment operator will not modify it.) – James Kanze Jan 06 '15 at 11:05
I'm sure you already found dozens of posts regarding the difference between a copy constructor and an assignment operator, or at least their default behavior (if you overload them, there's no limit to what you can do...or break). So I'll limit my answer to your specific question about the final outcome.
The outcome of calling either of them is that you end up with initialized (or reinitialized) data or object... However there is more to it than that because they are called in different circumstances and affect memory differently. Without going into long discussions about possible unwanted behaviors, stack usage, shallow vs deep copy (you need to be careful about this when dealing with pointers), the copy constructor will create a new instance of an object and copy data to it, while the assignment operator works with and already existing copy.
I hope this answers your question...

- 4,843
- 3
- 27
- 44