Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2014
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.65 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. class Base {
  4.     public: virtual ~Base() { }
  5. };
  6.  
  7. class Derived : public Base {
  8.     public: int field;
  9. };
  10.  
  11. void test(Base *ptr) {
  12.     Derived *d = dynamic_cast<Derived *>(ptr);
  13.    
  14.     if (d) {
  15.         std::cout << "field: " << d->field << std::endl;
  16.     } else {
  17.         std::cout << "Not a Derived object" << std::endl;
  18.     }
  19. }
  20.  
  21. int main() {
  22.     Base b;
  23.     Derived d;
  24.    
  25.     test(&b);
  26.     test(&d);
  27. }
  28.  
  29.  
  30. -----------------------------------------------------------
  31.  
  32. $ g++ -fno-rtti test.cpp -o test
  33. test.cpp: In function ‘void test(Base*):
  34. test.cpp:12:42: error:dynamic_cast’ not permitted with -fno-rtti
  35.   Derived *d = dynamic_cast<Derived *>(ptr);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement