FAQ 27.03 What is dynamic type checking?

Dynamic type checking, sometimes known as dynamic typing, is the determination of type correctness at runtime.

With dynamic type checking, user code determines whether an object supports a particular member function at runtime rather than at compile time. Dynamic type checking is often accompanied by downcasts (see FAQ 27.11) and can unnecessarily increase the cost of C++ software. Runtime type identification (RTTI) is one kind of dynamic type checking that is supported directly by C++.

The following example demonstrates the wrong way to do things. Pretend that the various escape sequences toggle italics on the various kinds of printers.

 #include <iostream> using namespace std; // pretend this is the escape character const char* const esc = "ESC"; enum Type { EPSON, PROPRINTER, STAR }; class Printer { public:   virtual ~Printer() throw();   virtual Type type() const throw() = 0; }; Printer::~Printer() throw() { } class EpsonPrinter : public Printer { public:   virtual Type type() const throw();   void italicsEpson(const char* s) throw(); }; Type EpsonPrinter::type() const throw() { return EPSON; } void EpsonPrinter::italicsEpson(const char* s) throw() { cout << esc << "i+" << s << esc << "i-"; } class ProprinterPrinter : public Printer { public:   virtual Type type() const throw();   void italicsProprinter(const char* s) throw(); }; Type ProprinterPrinter::type() const throw() { return PROPRINTER; } void ProprinterPrinter::italicsProprinter(const char* s) throw() { cout << esc << "[i" << s << esc << "[n"; } class StarPrinter : public Printer { public:   virtual Type type() const throw();   void italicsStar(const char* s) throw(); }; Type StarPrinter::type() const throw() { return STAR; } void StarPrinter::italicsStar(const char* s) throw() { cout << esc << "x" << s << esc << "y"; } void printUsingItalics(Printer& p, const char* s) throw() {   switch (p.type()) {     case EPSON:       ((EpsonPrinter&) p).italicsEpson(s);       break;     case PROPRINTER:       ((ProprinterPrinter&) p).italicsProprinter(s);       break;     case STAR:       ((StarPrinter&) p).italicsStar(s);       break;     default:       cerr << "Call tech support at 1-800-BAD-BUGS\n";   } } 

Although the example uses classes and virtual functions, it is not the best use of OO technology. The type() member function is used in basically the same way that procedural code uses tagged unions (that is, tag fields that indicate which piece of the union is currently being used). This approach is subject to error and is nonextensible compared to the proper use of classes and virtual functions, shown later in this chapter. For example, adding a new kind of printer requires changes to the printUsingItalics() function and probably to other functions as well. This is a ripple effect that is typical with non-OO (and bad OO!) software.



C++ FAQs
C Programming FAQs: Frequently Asked Questions
ISBN: 0201845199
EAN: 2147483647
Year: 2005
Pages: 566
Authors: Steve Summit

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net