alan.chen alan.chen - 8 months ago 56
C++ Question

What's the difference between assignment operator and copy constructor?

I don't understand the difference between assignment constructor and copy constructor in C++. It is like this:

class A {
A() {
cout << "A::A()" << endl;

// The copy constructor
A a = b;

// The assignment constructor
A c;
c = a;

// Is it right?

I want to know how to allocate memory of the assignment constructor and copy constructor?
Thank you!

sbi sbi
Answer Source

A copy constructor is used to initialize a previously uninitialized object from some other object's data.

A(const A& rhs) : data_(rhs.data_) {}

An assignment operator is used to replace the data of a previously initialized object with some other object's data.

A& operator=(const A& rhs) {data_ = rhs.data_; return *this;}

You could replace copy construction by default construction plus assignment, but that would be less efficient.

(As a side note: My implementations above are exactly the ones the compiler grants you for free, so it would not make much sense to implement them manually. If you have one of these two, it's likely that you are manually managing some resource. In that case, per The Rule of Three, you'll very likely also need the other one plus a destructor.)