Inheritance & Destructors
Constructing/Destructing Object built/destroyed in layers Base class constructed Derived class constructed Geometric Object Circle
Constructing/Destructing Object built/destroyed in layers Base class constructed Derived class constructed Derived class destroyed – clean up after itself Geometric Object
Constructing/Destructing Object built/destroyed in layers Base class constructed Derived class constructed Derived class destroyed – clean up after itself Base class destroyed – clean up after itself
Example Simple list has array of ints LessSimpleList is a SimpleList with one extra char on heap Each new has a matching delete
Example Base created Derived created Derived destroyed Base destroyed 1 5 3 2 4
Example Base created Derived created Derived destroyed Base destroyed 1 5 3 2 4
Copy Constructor Issue No copy constructor = trouble:
Copy Constructor Issue Copying child class defaults to no-arg construction of parent:
Copy Constructor Fix Child copy ctor should explicitly call parent one
Assignment Operator Assignment operator also needs to call parent version:
Virtual Messiness Also need to worry about virtual dispatch to handle: SimpleList* list = new LessSimpleList(); delete list; Fixes: Virtual destructor Child classes need to override parent Copy/Assignment https://stackoverflow.com/questions/669818/virtual-assignment-operator-c