fork download
  1. #include <iostream>
  2. #include <typeinfo>
  3.  
  4. struct A
  5. {
  6. virtual ~A() {}
  7. };
  8.  
  9. struct B : A
  10. {
  11.  
  12. };
  13.  
  14. struct C : A
  15. {
  16.  
  17. };
  18.  
  19. C *c = new C;
  20.  
  21. B *b = new B;
  22.  
  23. A *a = new A;
  24.  
  25. void foo (A *a)
  26. {
  27. if(dynamic_cast<B*>(a))
  28. {
  29. std::cout << "Type is B*" << std::endl;
  30. //cannot dynamic_cast 'a' (of type 'struct A*') to type 'struct B*' (source type is not polymorphic)
  31. }
  32.  
  33. if(dynamic_cast<C*>(a))
  34. {
  35. std::cout << "Type is C*" << std::endl;
  36. //cannot dynamic_cast 'a' (of type 'struct A*') to type 'struct C*' (source type is not polymorphic)
  37. }
  38. }
  39.  
  40. int main()
  41. {
  42. foo(b);
  43. foo(c);
  44. return 0;
  45. }
Success #stdin #stdout 0s 3272KB
stdin
Standard input is empty
stdout
Type is B*
Type is C*