21-polymorpic-containers

21-polymorpic-containers - Sheet1 Page 1 Last Time: *...

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

View Full Document Right Arrow Icon

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

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

Unformatted text preview: Sheet1 Page 1 Last Time: * Containers-by-reference vs. by-value Today: * Run-time polymorphism **************************************************** Templates are checked at compile time, but when used straightforwardly, they cannot hold more than one kind of object at once, and sometimes this is desireable. There is another kind of container, called a "polymorphic" container, that *can* hold more than one type at once, but (as a result) cannot be type-checked at compile time. The intuition behind polymorphic containers is that---because the container must contain *some* type---we'll manufacture a special contained type, and every real type will be a subtype of this contained type. To think about this, remember what *subtyping* was all about: S is a subtype of T if S is substitutable for T. The C++ mechanism to implement subtypes is the "derived class". class foo : public bar { ... } Then a foo * can always be used where a bar * is expected, but not the other way around. We can take advantage of this by creating a "dummy class", called Object, that looks like this: class Object { public: virtual ~Object() { } } Which defines a single class Object with a virtual destructor. Now, we can write a List that holds Objects: struct node { node *next node *prev Object *value } Sheet1 Page 2 class List { ... public: void insert(Object *o) Object *remove() ......
View Full Document

Page1 / 12

21-polymorpic-containers - Sheet1 Page 1 Last Time: *...

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

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