Rule of Five
Rule of 3 If you manage memory you need: Custom Destructor Custom Copy Constructor Custom Assignment Operator
Rule of 5 If you manage memory you need: You may want to provide: Custom Destructor Custom Copy Constructor Custom Assignment Operator You may want to provide: Move assignment operator Move copy constructor
Move semantics C++ allows us to specify that data should be moved from one place to another:
Moving Copy:
Moving Copy:
Moving Copy:
Moving Move:
Moving Move:
Moving Move:
Defining && = Rvalue reference Move constructor/assignment take rvalue reference: Move constructor:
Defining && = Rvalue reference Move constructor/assignment take rvalue reference: Move assignment:
Rule of 5 Why? But… Move can be more efficient than copying Never required for program correctness, just efficiency