-1

We can implement our own copy constructor if we don't want a shallow copy. So copy constructurs and copy assignment operators are used to implement deep copy.

What sort of relation/interaction do move constructors and assignment operators have with the concepts of deep and shallow copy?

Am I right in saying that move-constructors are used when we don't want a deep copy, but rather a shallow copy (and subsequently transfering ownership)?

JohnDo
  • 9
  • 1
  • 2
  • Move constructor/assignment operator is used when we do not want a copy (neither shallow nor deep), but we want to move (transfer) contents from one object to another, leaving the moved-from object without its original contents. Move constructors do not perform shallow copy in general. – Daniel Langr Oct 02 '18 at 10:23
  • `Move constructor` isn't called `Move and copy constructor` for a reason.. It simply moves an object owner . – sagi Oct 02 '18 at 10:27
  • 5
    You could think of it as a *destructive shallow copy*, where the source is left in an *empty* state – Caleth Oct 02 '18 at 10:34

1 Answers1

1

Move construction/assignment is not a logical copy at all. They're transferring ownership; typically, they're implemented similarly to a shallow copy (thus the performance boost), but since the original object is usually emptied in the process, it's not really a copy at all.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271