Advertisement
jacknpoe

Teste de Dynamic Cast de Classes

Mar 3rd, 2016
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <ostream>
  3.  
  4. class base {
  5.     public:
  6.     virtual ~base( ) {}
  7. };
  8.  
  9. class derived : public base {};
  10. class most_derived : public derived {};
  11. class other : public base {};
  12.  
  13. int main( )
  14. {
  15.     base* b = new derived;
  16.     std::cout << "A " << b << "\n";
  17.     std::cout << "B " << dynamic_cast<most_derived*>(b) << "\n"; // Null pointer
  18.     dynamic_cast<derived&>(*b); // OK
  19.     std::cout << "C " << b << "\n";
  20.     std::cout << "D " << dynamic_cast<other*>(b) << "\n"; // Null pointer
  21.     derived* d = new most_derived;
  22.     std::cout << "E " << d << "\n";
  23.     b = d;
  24.     std::cout << "F " << b << "\n";
  25.     b = dynamic_cast<base*>(d); // OK, but dynamic_cast<> is unnecessary.
  26.     std::cout << "G " << b << "\n";
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement