Download presentation
Presentation is loading. Please wait.
1
Rule of Three Part 1 & 2
2
Rule of Three Any object which manages memory needs: Custom Destructor
Custom Copy Constructor Custom Assignment Operator
3
Destructor Destructor : Special function called when object destroyed
Automatically called Almost never called by hand ~CLASSNAME()
4
Destructors Stack objects destroyed when leave scope:
5
Destructors Heap objects must be destroyed manually:
6
Cleaning Up Memory allocated by object = potential leak
7
Cleaning Up Memory allocated by object = potential leak
8
Destructor Destructor used to clean up owned resources:
New in constructor delete in destructor
9
Copy Constructor Every class has a default implicit copy constructor
Class::Class (const Class &other) Created invisibly and automatically
10
Copy Constructor Using copy constructor: p2 is copying p1
11
Copy Constructor Default works for simple objects: p1 p2 x: 4 y: 3
12
The Issue Default copy constructor for Polygon
13
The Issue Copies sides
14
The Issue Copies vertices pointer
15
The Issue Two objects now share one list of points
16
Shallow copy Shallow copy : duplicate pointers of an object
Copy shares memory with original
17
Deep copy Deep copy : duplicate memory owned by original
Copy gets own version of data
18
Copy Constructor Deep copy constructor Copy basic variables
Allocate own storage Copy data to it
19
Copy Constructor Deep copy constructor Copy basic variables
Allocate own storage Copy data to it
20
Copy Constructor Deep copy constructor Copy basic variables
Allocate own storage Copy data to it
21
Copy Constructor Deep copy constructor Copy basic variables
Allocate own storage Copy data to it
22
Book Sample
23
Book Example Course class introduced in Ch 11
24
Book Example Constructor allocates array Destructor deletes
25
Book Example Book Ex11.20 copy constructor
Has typo – missing last 2 lines in book Should be:
26
Copy Constructor Deep copy recipe: Copy basic variables
Allocate own storage/resources Copy data from other to own storage
27
Copy Constructor Recipee
For Course:
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.