1 / 28

The Assignment Operator

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:.

irisa
Download Presentation

The Assignment Operator

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Assignment Operator

  2. Rule of Three • Any object which manages memory needs: • Custom Destructor • Custom Copy Constructor • Custom Assignment Operator (now)

  3. Copy Constructor • Generally need a deep copy not a shallow one: YES NO

  4. Copy Constructor Recipee • Basic copy constructor recipe:

  5. Using Copy Ctor Explicitly • Explicit copy constructor call:

  6. Using Copy Ctor Implicitly • Initialization while declaring uses copy constructor:

  7. Using Assignment Operator • Assign existing object uses assignment operator

  8. Default Assignment Operator • Every class gets default assignment operator • Copies each member directly: • Shallow copy!!!

  9. 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

  10. 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

  11. 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)

  12. Why return reference • Chained assignments are allowed in C++ Only makes sense if y = 2 resolves to y when done

  13. Why return reference • Chained assignments are allowed in C++ Only makes sense if y = 2 resolves to y when done y

  14. 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

  15. Copy Constructor First Pass… Copy basics:

  16. Copy Constructor First Pass… Delete oldstorage

  17. Copy Constructor First Pass… Allocate newstorage andcopy data

  18. Copy Constructor First Pass… Return theobject that wascopied into

  19. Self Assignment Danger • Self assignment will clear an object / blow up your program:

  20. Bad Self Assignment Copying basicinfo does nothing

  21. Bad Self Assignment Deleting wipes outstorage!!!

  22. Bad Self Assignment Allocate a newempty array

  23. Bad Self Assignment Loop either copiesempty objectsor maybe blowsup

  24. Bad Self Assignment Loop either copiesempty objectsor maybe blowsup

  25. Final Version • Final version : • Compare address of self to address of otherOnly copy if not same

  26. Recipe • Basic recipe:

  27. Applied to ArrayList • Combine operator overloads with templates and we get a much easier to use array:

  28. 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

More Related