The Assignment Operator
Rule of Three Any object which manages memory needs: Custom Destructor Custom Copy Constructor Custom Assignment Operator
Copy Constructor Generally need a deep copy not a shallow one: YES NO
Copy Constructor Recipee Basic copy constructor recipe:
Using Copy Ctor Explicitly Explicit copy constructor call:
Using Copy Ctor Implicitly Initialization while declaring uses copy constructor:
Using Assignment Operator Assign existing object uses assignment operator
Default Assignment Operator Every class gets default assignment operator Copies each member directly: Shallow copy!!!
Custom Assignment Operator Custom assignment operator similar to copy constructor Copy over basic information Allocate own dynamic memory Copy values that are in dynamic memory
Custom Assignment Operator Custom assignment operator similar to copy constructor Copy over basic information Allocate own dynamic memory Copy values that are in dynamic memory Differences: Usually returns a reference to the object Already have an object… which already owns memory
Custom Assignment Operator Course assignment operator: I will return a reference to the course that we are copying into I take a reference to a course we will not change Interpreted as course2.=(course1)
Why return reference Chained assignments are allowed in C++ Only makes sense if y = 2 resolves to y when done
Why return reference Chained assignments are allowed in C++ y Only makes sense if y = 2 resolves to y when done y
Assignment First Pass… On assignment: Two existing objects Want to turn one into deep copy of other course2 = course1 course2.=(other) course2 is this course1 is other
Flawed first attempt… Copy basic stuff
Flawed first attempt… Delete old storage
Flawed first attempt… Allocate new storage
Flawed first attempt… Copy data
Flawed first attempt… Return the object that was copied into
Self Assignment Danger Flaw : self assignment deletes data
Bad Self Assignment Copying basic info does nothing
Bad Self Assignment Deleting wipes out storage!!!
Bad Self Assignment Allocate a new empty array
Bad Self Assignment Loop either copies empty data or blows up array up
Final Version Avoid modifying on self assignment: If address of other is same as this (my address), don't copy
Recipe Basic recipe: Step Pseudocode Verify not self assigning Copy basics Delete old memory Make new array Copy array No matter what, return self if( this != &other) { this.size = other.size … delete [] array array = new whatever[other.size] for(i = 0 to other.size -1) array[i] = other.array[i] } return *this
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: