22+-+Polymorphism+and+Bugs

22+-+Polymorphism+and+Bugs - EECS 280 Programming and...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Polymorphism and Bugs EECS 280 Programming and Introductory Data Structures 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Containers Templates 2 You may have noticed that we declared the BigThing list as: List<BigThing> *lbtp = new List<BigThing>; and NOT : List<BigThing *> *lbtp = new List<BigThing *>;
Background image of page 2
Containers Templates 3 We have introduced the "at-most-once" invariant to avoid the vast majority of these sorts of bugs. At-most-once: a pointer to any specific object can exist in at most one container object at any point in time. 1. Existence : An object must be dynamically allocated before a pointer to it is inserted. 2. Ownership : Once a pointer to an object is inserted, that object becomes the property of the container. No one else may use or modify it in any way.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Containers Templates 4 These three rules have an important implication for any method that destroys an existing container. There are (at least) two such methods: 1. The destructor: Destroys an existing instance. 2. The assignment operator: Destroys an existing instance before copying the contents of another instance.
Background image of page 4
Containers Templates 5 Consider the following implementation of the destructor for a singly-linked, simple list, using the interface we've discussed so far: template <class T> List<T>::~List() { while (!isEmpty()) { remove(); } int IntList::remove() { node *victim = first; int result; if (isEmpty()) { listIsEmpty e; throw e; } first = victim->next; result = victim->value; delete victim; return result; } // IntList remove() struct node { node *next; node *prev; T *o; }; Yes…I know this is for ints, not Ts
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Containers Templates 6 To fix this, we must handle the objects we remove: template <class T> List<T>::~List() { while (!isEmpty()) { T *op = remove(); delete op; } } This provides conservation.
Background image of page 6
Templates 7 The consequence of this is that the List template must know whether it is something that holds T ’s or “pointers to T ”. The former cannot delete values it held that are being destroyed, while the latter must do so. So, if we want to write a template class
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/19/2012 for the course EECS 280 taught by Professor Noble during the Winter '08 term at University of Michigan.

Page1 / 30

22+-+Polymorphism+and+Bugs - EECS 280 Programming and...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online