Destruction and Copying
Destructor Destructor : Special function called when object destroyed Automatically called Almost never called by hand ~CLASSNAME()
Destructors Stack objects destroyed when leave scope:
Destructors Heap objects must be destroyed manually:
Cleaning Up Memory allocated by object = potential leak
Cleaning Up Memory allocated by object = potential leak
Destructor Proper place to clean up memory object owns: New in constructor delete in destructor
Copy Constructor Every class has a default implicit copy constructor Class::Class (const Class &other) Created invisibly and automatically
Copy Constructor Using copy constructor: p2 is copying p1
The Issue Default copy constructor for Polygon
The Issue Default copy constructor for Polygon
The Issue Default copy constructor for Polygon
The Issue Default copy constructor for Polygon
Shallow copy Shallow copy : duplicate pointers of an object Copy shares memory with original
Deep copy Deep copy : duplicate memory owned by original Copy gets own version of data
Copy Constructor Deep copy constructor Allocate own storage Copy data to it
Copy Constructor Deep copy constructor Allocate own storage Copy data to it
Copy Constructor Deep copy constructor Allocate own storage Copy data to it
Copy Constructor Deep copy constructor Allocate own storage Copy data to it
Copy Constructor Deep copy constructor Allocate own storage Copy data to it
Copy Constructor Deep copy constructor Allocate own storage Copy data to it
Book Sample
Book Example Course maintains a list of student names:
Book Example Course.h
Book Example Book Ex11.20 copy constructor Has typo – missing last 2 lines in book Should be: