2

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.

So ultimately both are copying the contents from one object to another then why we need both we can have only one right.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Lokesh
  • 21
  • 3
  • 1
    The object might need to be destroyed before being reconstructed if the language had been designed to use copy reconstruction instead of assignment. An assignment operator might do what the destructor does followed by what the constructor does. Or an assignment operator might be able to skip some of each or in some other way get the same effect in less work. – JSF Dec 30 '15 at 11:04
  • I think the short answer is, copy constructor really needs to be there or pass by value doesn't work and other things don't work. Assignment operator, you might not need as compulsively, but in some cases you could go a lot faster if you are able to overload it, so here we are... Also its not always clear what the assignment operator should be. Sometimes you want it just to be `memcpy`, but if your object is a smart pointer, then that will make you a sad panda. If it's always "destroy and copy construct" well then the cases where you wanted `memcpy` will be slower. – Chris Beck Dec 30 '15 at 11:14

4 Answers4

1

You shouldn't create dummy objects just to do an assignment in the next step, every time you want a copy.

Especially if the object construction is expensive even when default constructed.

But the definitive reason for copy c'tors, is that pass by value for user defined types will be impossible. And then we will have no RAII.

As for assignment operators, imagine trying to write classes without it. How would generic (template) code look?

Many generic algorithms can operate on classes that are merely assignable, but without that feature, you'd have to call their d'tor yourself before "constructing" the objects again.

template <calls T)
void assign(T& a, T& b)
{
  a.~T();
  new (&a) (b);
}

Basically, that puts an unreasonable demand on classes that their d'tor must be public. Or they must have you algorithm as friend.

Furthermore, this is unsafe. Constructors may throw, that's how they report errors, and if the T copy c'tor does, than a is in an undefined state following the destructor call.
The problem is that you took the decision of freeing the state of a away from it. If we had used the assignment operator instead, the assignment would have still thrown, but it could have made sure that a doesn't free it's state yet.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
1

Because construction and assignment are different things. Copy constructor is involved, for example when you pass an argument by value or construct an object with another one :

f(T o) {
...}

T object;
f(object); // copy construction to pass the argument
T object2(object); // construct a T from another T

While assignment is when you already have both objects and want to copy one into the other:

T object;
...
T object2;
...
object = object2;

These things happens at different kind of expressions.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
1

Because they are different things.

Copy ctor will be called when you want to construct an new object from another one.

Assignment operator will be called when you want to assign an existing object from another one, that means you might need to destroy/release some resources which the existing object hold before doing the assignment.

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
0

There are two different cases here,

  • initializing a new object with contents of an existing object (1)
  • copy/transfer contents of an existing object to another existing object (2)

There are different scenarios where (1) is applicable and where (2) applies. They are not same.

For example, If we want to share ownership of resources(e.g. memory) owned an object with another object with same type (shared_ptr is a good example), we need a proper copy-assignment operator implemented on that class.

Hence, we need provision for both copy construction and copy assignment.

Pravar Jawalekar
  • 605
  • 1
  • 6
  • 18