Skip to Main Content
Language Implementation Patterns
book

Language Implementation Patterns

by Terence Parr
December 2009
Intermediate to advanced content levelIntermediate to advanced
380 pages
9h 2m
English
Pragmatic Bookshelf
Content preview from Language Implementation Patterns
Pattern 23Enforcing Polymorphic Type Safety

Purpose

This pattern detects type incompatibilities in object pointer assignments in object-oriented languages like C++.

Discussion

Beyond the type checking done in Pattern 22, Enforcing Static Type Safety, object-oriented languages have one more type hazard to worry about: polymorphic object pointer assignment. Polymorphism means that a pointer can refer to objects of multiple types. In contrast, C pointer assignments have to be between exact types. The key difference then between this pattern and the previous lies in the definition of type compatibility. Let’s take a look at a C++ example that demonstrates what we need to handle:

 
Cat *pCat;
 
Bengal *pBengal; ​// assume Bengal subclasses Cat
 
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Implementation Patterns

Implementation Patterns

Kent Beck

Publisher Resources

ISBN: 9781680500097Errata Page