fork download
  1. #include <iostream>
  2.  
  3. struct A
  4. {
  5. int a_;
  6.  
  7. A(int a)
  8. : a_(a)
  9. {
  10. }
  11.  
  12. virtual ~A()
  13. {
  14. }
  15. };
  16.  
  17. struct B :
  18. public A
  19. {
  20. int b_;
  21.  
  22. B(int a, int b)
  23. : A(a)
  24. , b_(b)
  25. {
  26. }
  27. };
  28.  
  29. struct C :
  30. public A
  31. {
  32. int c_;
  33.  
  34. C(int a, int c)
  35. : A(a)
  36. , c_(c)
  37. {
  38. }
  39. };
  40.  
  41. struct D
  42. {
  43. int d_;
  44.  
  45. D()
  46. : d_(0)
  47. {
  48. }
  49.  
  50. virtual int d()
  51. {
  52. return d_;
  53. }
  54. };
  55.  
  56. struct E :
  57. public D,
  58. public A
  59. {
  60. E()
  61. : D()
  62. , A(0)
  63. {
  64. }
  65. };
  66.  
  67. template <class Base, class Derived>
  68. bool is_differ(const Derived* der)
  69. {
  70. return (void*) der != (Base*) der;
  71. }
  72.  
  73. int main()
  74. {
  75. A* a = new A(5);
  76. B* b = new B(2, 3);
  77. C* c = new C(1, 4);
  78. E* e = new E();
  79.  
  80. std::cout << std::boolalpha;
  81. std::cout << "A* and B* differ: " << is_differ<A, B>(b) << std::endl;
  82. std::cout << "A* and C* differ: " << is_differ<A, C>(c) << std::endl;
  83. std::cout << "A* and E* differ: " << is_differ<A, E>(e) << std::endl;
  84. return 0;
  85. }
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
A* and B* differ: false
A* and C* differ: false
A* and E* differ: true