Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Assignment Operator. Rule of Three Any object which manages memory needs: – Custom Destructor – Custom Copy Constructor – Custom Assignment Operator.

Similar presentations


Presentation on theme: "The Assignment Operator. Rule of Three Any object which manages memory needs: – Custom Destructor – Custom Copy Constructor – Custom Assignment Operator."— 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: YESNO

4 Copy Constructor Recipee Basic copy constructor recipe:

5 Using Copy Ctor Explicitly Explicit copy constructor call:

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

7 Assign existing object uses assignment operator Using 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: – Usually returns a reference to the object – Already have an object… which already owns memory

11 Custom Assignment Operator Course assignment operator: Interpreted as course2.=(course1) I take a reference to a course we will not change I will return a reference to the course that we are copying into

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

15 Flawed first attempt… Copy basic stuff

16 Flawed first attempt… Delete old storage

17 Flawed first attempt… Allocate new storage

18 Flawed first attempt… Copy data

19 Flawed first attempt… Return the object that was copied into

20 Self Assignment Danger Flaw : self assignment deletes data

21 Bad Self Assignment Copying basic info does nothing

22 Bad Self Assignment Deleting wipes out storage!!!

23 Bad Self Assignment Allocate a new empty array

24 Bad Self Assignment Loop either copies empty data or blows up array up

25 Final Version Avoid modifying on self assignment: – If address of other is same as this (my address), don't copy

26 Recipe Basic recipe: StepPseudocode 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

27 Rule of 3 If you manage memory you need: – Custom Destructor – Custom Copy Constructor – Custom Assignment Operator

28 Rule of 5 If you manage memory you need: – Custom Destructor – Custom Copy Constructor – Custom Assignment Operator You may want to provide: – Move assignment operator – Move copy constructor

29 Move semantics C++ allows us to specify that data should be moved from one place to another:

30 Moving Copy:

31 Moving Copy:

32 Moving Copy:

33 Moving Move:

34 Moving Move:

35 Moving Move:

36 Defining && = Rvalue reference Move constructor/assignment take rvalue reference: – Move constructor:

37 Defining && = Rvalue reference Move constructor/assignment take rvalue reference: – Move assignment:


Download ppt "The Assignment Operator. Rule of Three Any object which manages memory needs: – Custom Destructor – Custom Copy Constructor – Custom Assignment Operator."

Similar presentations


Ads by Google