Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of copy constructor while the same can be done with assignment operator '='?

Why C++ provide a copy constructor? The assignment operator can do the same task. Is there any advantage of copy constructor over assignment operator?

like image 421
Proton Avatar asked Feb 23 '21 20:02

Proton


People also ask

Why copy constructor and assignment operator's working is different?

The Copy constructor and the assignment operators are used to initializing one object to another object. The main difference between them is that the copy constructor creates a separate memory block for the new object. But the assignment operator does not make new memory space.

Why a class should have both a copy constructor and an overloaded assignment operator?

Both the copy constructors and assignment operators copies from one object to another then why do we need both? Copyconstructor means it creates a new object and copies the contents from another exist object and assignment operator copies the contents from one existing object to the already existing object.

Can you call copy constructor in assignment operator?

You can safely invoke the copy assignment operator from the constructor as long as the operator is not declared virtual.


3 Answers

Stuff you can do with a copy constructor that you can't do (either easily or at all) with the assignment operator:

  1. Copy classes that don't have a default constructor. For example, if a class represents an open file, you might not be able to construct one without passing it a file name to open.

  2. Copy classes that have an expensive default constructor. Maybe the constructor allocates a lot of memory, which will then be released as soon as you use the assignment operator to copy a new state into the object.

  3. Pass an instance of the class by value. This is kind of the original purpose of the copy constructor. In C, if you pass a struct by value, the compiler just does a bitwise copy of the struct so the receiving function has a local copy that it can modify without affecting the caller. But C++ recognizes that a bitwise copy is not the best way to copy most objects, so it lets you write your own copy constructor (and the default copy behavior is different too, since class members may have custom copy constructors).

  4. Copy a class that contains references, because you can't reassign a reference after the class has already been constructed. The copy constructor and assignment operator just do different things where references are concerned. The copy constructor initializes the reference to point to the same object that the reference points to in the instance that is being copied; the assignment operator actually copies the value of the referenced object.

  5. Copy a class with const members. (Note that a class can have a default constructor but still have const members.)

like image 68
Willis Blackburn Avatar answered Oct 11 '22 18:10

Willis Blackburn


With or without a copy constructor, you still have to initialize a new object to a stable initial state, which the assignment operator can then update later.

While you can certainly do that without a copy constructor, having a copy constructor helps to optimize a new object's initialization, by setting it to copy another object's state up front, without requiring you to first initialize the new object to a default state and then have a separate assignment reset that state afterwards. This way, you can set the new object's state in 1 operation instead of 2 operations.

like image 26
Remy Lebeau Avatar answered Oct 11 '22 18:10

Remy Lebeau


Yes, the two are different. You can't always just implement your copy constructor as

Foo(const Foo& f) {
    *this = f;
}

The assignment operator assumes that you have a valid, fully constructed object. The copy constructor makes no such assumptions. This means that, depending on your class, the assignment operator may try to clear whatever data is on the object before re-initializing. Or may even repurpose the data already on the object.

like image 40
scohe001 Avatar answered Oct 11 '22 17:10

scohe001