350 likes | 841 Views
The Assignment Operator. Rule of Three. Any object which manages memory needs: Custom Destructor Custom Copy Constructor Custom Assignment Operator (now). Copy Constructor. Generally need a deep copy not a shallow one: YES NO. Copy Constructor Recipee. Basic copy constructor recipe:.
E N D
Rule of Three • Any object which manages memory needs: • Custom Destructor • Custom Copy Constructor • Custom Assignment Operator (now)
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: • Signature : returns a object reference • Already have an object… which already owns memory • Have to watch for self assignment
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++ Only makes sense if y = 2 resolves to y when done y
Copy Constructor First Pass… • On assignment: • Two existing objects • Want to turn one into deep copy of other course2 = course1course2.=(other)course2 is thiscourse1 is other
Copy Constructor First Pass… Copy basics:
Copy Constructor First Pass… Delete oldstorage
Copy Constructor First Pass… Allocate newstorage andcopy data
Copy Constructor First Pass… Return theobject that wascopied into
Self Assignment Danger • Self assignment will clear an object / blow up your program:
Bad Self Assignment Copying basicinfo does nothing
Bad Self Assignment Deleting wipes outstorage!!!
Bad Self Assignment Allocate a newempty array
Bad Self Assignment Loop either copiesempty objectsor maybe blowsup
Bad Self Assignment Loop either copiesempty objectsor maybe blowsup
Final Version • Final version : • Compare address of self to address of otherOnly copy if not same
Recipe • Basic recipe:
Applied to ArrayList • Combine operator overloads with templates and we get a much easier to use array:
Vectors • Vector is C++'s official ArrayList • Programmers should use instead of arrays most of the time • We'll study week 10 • Take a peek if you want Ch 12.6-12.8