Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <typeinfo>
  3. using namespace std;
  4.  
  5. class A {
  6.     public:
  7.         virtual void fx() {
  8.             cout << "A::fx" << endl;
  9.         }
  10. };
  11.  
  12. class B: public A {
  13.     public:
  14.         void fx() {
  15.             cout << "B::fx" << endl;
  16.         }
  17. };
  18.  
  19. class C: public A {
  20.     public:
  21.         void fx () {
  22.             cout << "C::fx" << endl;
  23.         }
  24. };
  25.  
  26. void f(A* p, A& r) {
  27.     if ((bool)(B& pr = dynamic_cast <B&> (r))) {
  28.         cout << "using of pp" << endl;
  29.     } else {
  30.         cout << "NULL" << endl;
  31.     }
  32.             cout << "STILL HERE" << endl;
  33.     B* pp = dynamic_cast <B*> (p);
  34. }
  35.  
  36. void g() {
  37.     try {
  38.         cout << "f(new B, *new B) - correct using" << endl;
  39.         f(new B, *new B);
  40.         cout << "f(new C, *new C) - incorrect using" << endl;
  41.         f(new C, *new C);
  42.         cout << "STILL HERE" << endl;
  43.     }
  44.     catch(bad_cast) {
  45.         cout << "Bad_cast" << endl;
  46.     }
  47. }
  48.  
  49. int main() {
  50.     g();
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement