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

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.20134 Мб27A.Menezes, P.van Oorschot,S.Vanstone - HANDBOOK OF APPLIED CRYPTOGRAPHY.djvu
  • #
  • #
    08.05.20136 Мб30B.Eckel - Thinking in Java, 3rd edition (beta).pdf
  • #
  • #
    08.05.20136 Мб23D.MacKay - Information Theory, Inference, and Learning Algorithms.djvu
  • #
    08.05.20134 Мб21DIGITAL Visual Fortran ver.5.0 - Programmers Guide to Fortran.djvu
  • #
    08.05.20132 Мб17E.A.Lee, P.Varaiya - Structure and Interpretation of Signals and Systems.djvu