Some details of the class have been removed. In particular, the constructor dynamically allocates the DataStructure
object and the destructor deallocates it. If, during a move, you just copied the pointer from one Widget
to another, both Widget
s would have pointers to the same allocated DataStructure
object. Then, when those objects are destroyed, they would both attempt to delete
it. This would give undefined behaviour. To avoid this, the Widget
that is being moved from has its internal pointer to set to nullptr
.
This a standard pattern when implementing a move constructor. You want to move ownership of some dynamically allocated objects from one object to another, so you need to make sure the original object no longer owns those allocated objects.
Diagrammatically, you start off with this situation, wanting to move ownership of the DataStructure
from one Widget
to the other:
┌────────┐ ┌────────┐
│ Widget │ │ Widget │
└───╂────┘ └────────┘
┃
▼
┌───────────────┐
│ DataStructure │
└───────────────┘
If you just copied the pointer, you'd have:
┌────────┐ ┌────────┐
│ Widget │ │ Widget │
└───╂────┘ └───╂────┘
┗━━━━━━━━┳━━━━━━━┛
▼
┌───────────────┐
│ DataStructure │
└───────────────┘
If you then set the original Widget
pointer to nullptr
, you have:
┌────────┐ ┌────────┐
│ Widget │ │ Widget │
└────────┘ └───╂────┘
┃
▼
┌───────────────┐
│ DataStructure │
└───────────────┘
Ownership has successfully been transferred, and when both Widget
s can be destroyed without causing undefined behaviour.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…