copy constructors

hey guys i have a question

why do we need copy assignment operator if we have copy constructor doing the job???

please tell me ??
A copy constructor lets you do
1
2
3
//given a of type T
T b(a);
T c = a;
While an assignment operator lets you do
 
c = a;
oh helios if you plz explain this ....


what i see is that in both statements it is copying a into c .......

plz tell me if i m wrong???
correct me plzzzz
The copy constructor is used when you create a new object that is a copy of another object.

The assignment operator is used when you make an existing object a copy of another object.
Yes, both statements copy the contents of a into c, BUT C++ has different kinds of mechanisms that do this.

In the first snippet, both lines are forms of copy construction. That is, T c = a; and T c(a); mean the same thing.

In the second snippet, that's regular assignment. It's distinguished from copy construction because c already existed before that statement. The distinction between initializing an object to a certain value and changing the value of an object after it has been created is important for some types.
Topic archived. No new replies allowed.