fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <typeinfo>
  4. using namespace std;
  5.  
  6. template <class T>
  7. void clearVec ( vector<T> vec)
  8. {
  9. //for(typename vector<T>::iterator it = vec.begin(); it != vec.end(); ) {
  10. //for_each(vec.begin(), vec.end(), delete) {
  11. // delete *it;
  12. //vec.erase(it);
  13. //}
  14. //vec.clear();
  15. }
  16.  
  17. class A
  18. {
  19. public:
  20. A()
  21. {
  22. cout << "Constructor A\n";
  23. }
  24. virtual ~A()
  25. {
  26. cout << "Destructor A\n";
  27. }
  28. virtual void update()
  29. {
  30. func ();
  31. func2 ();
  32. }
  33.  
  34. virtual void func()
  35. {
  36. cout << "func A\n";
  37. func2();
  38. }
  39.  
  40. virtual void func2()
  41. {
  42. cout << "func2 A\n";
  43. }
  44. virtual double foo(unsigned a) const
  45. {
  46.  
  47. return 2.5;
  48. }
  49. };
  50.  
  51. class B: public A
  52. {
  53. public:
  54. B():A()
  55. {
  56. cout << "Constructor B\n";
  57. }
  58. virtual ~B()
  59. {
  60. cout << "Destructor B\n";
  61. }
  62. //virtual void func()
  63. //{
  64. // A::func();
  65. // cout << "func B\n";
  66. //}
  67.  
  68. void func2()
  69. {
  70. cout << "func2 B\n";
  71. }
  72. };
  73.  
  74. typedef double (A::*Method)(unsigned) const;
  75.  
  76. void test(A* a, Method mtd){
  77. unsigned b = 5;
  78. cout << "test met " << (a->*mtd)(b) << endl;
  79. }
  80.  
  81. template<class T>
  82. bool isSameObject(T const * p, T const * q)
  83. {
  84. //T* a = const_cast<T*>(p);
  85. //T* b = const_cast<T*>(q);
  86. //return dynamic_cast<T*>(a) == dynamic_cast<T*>(b);
  87.  
  88. //return dynamic_cast<const T*>(p) == dynamic_cast<const T*>(q);
  89. return dynamic_cast<T const*>(p) == dynamic_cast<T const*>(q);
  90. }
  91.  
  92. int main() {
  93. // your code goes here
  94. //B* b = new B();
  95. //A* a = new B();
  96. //b->update();
  97. //a->update();
  98. //delete a;
  99. //delete b;
  100. //vector<A*> v;
  101. //v.push_back(new B());
  102. //v.push_back(new A());
  103.  
  104. //for(vector<A*>::iterator it = v.begin(); it != v.end(); ++it ) {
  105. //for(size_t it = 0; it < v.size(); ++it ) {
  106. //A* a = v[it];
  107. // A* a = *it;
  108. //delete a;
  109. //}
  110. //clearVec(v);
  111. //v.clear();
  112. A* a = new A();
  113. A* b = new B();
  114. b->func();
  115.  
  116. cout << b << endl;
  117. //cout << typeid(*(dynamic_cast<void*>(b))).name();
  118.  
  119. cout << isSameObject(b, b);
  120.  
  121.  
  122. test(a, &A::foo);
  123. return 0;
  124. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Constructor A
Constructor A
Constructor B
func A
func2 B
0x55afa723eea0
1test met 2.5