Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
B.Eckel - Thinking in C++, Vol.2, 2nd edition.pdf
Скачиваний:
50
Добавлен:
08.05.2013
Размер:
2.09 Mб
Скачать

void pointers

Run-time type identification doesn’t work with void pointers:

//: C08:Voidrtti.cpp

// RTTI & void pointers #include <iostream> #include <typeinfo> using namespace std;

class Stimpy { public:

virtual void happy() {} virtual void joy() {} virtual ~Stimpy() {}

};

int main() {

void* v = new Stimpy; // Error:

//! Stimpy* s = dynamic_cast<Stimpy*>(v); // Error:

//! cout << typeid(*v).name() << endl; } ///:~

A void* truly means “no type information at all.”

Using RTTI with templates

Templates generate many different class names, and sometimes you’d like to print out information about what class you’re in. RTTI provides a convenient way to do this. The following example revisits the code in Chapter XX to print out the order of constructor and destructor calls without using a preprocessor macro:

//: C08:ConstructorOrder.cpp // Order of constructor calls #include <iostream>

#include <typeinfo> using namespace std;

template<int id> class Announce { public:

Announce() {

cout << typeid(*this).name()

Chapter 17: Run-Time Type Identification

408

Соседние файлы в предмете Численные методы
  • #
    08.05.20133.99 Mб22A.Menezes, P.van Oorschot,S.Vanstone - HANDBOOK OF APPLIED CRYPTOGRAPHY.djvu
  • #
  • #
    08.05.20135.91 Mб24B.Eckel - Thinking in Java, 3rd edition (beta).pdf
  • #
  • #
    08.05.20136.09 Mб17D.MacKay - Information Theory, Inference, and Learning Algorithms.djvu
  • #
    08.05.20133.85 Mб15DIGITAL Visual Fortran ver.5.0 - Programmers Guide to Fortran.djvu
  • #
    08.05.20131.84 Mб12E.A.Lee, P.Varaiya - Structure and Interpretation of Signals and Systems.djvu