fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template<class T>
  5. struct inspect_t
  6. {
  7. T* data;
  8. inspect_t(T* v):data(v) {}
  9. };
  10.  
  11. template<class T>
  12. inline inspect_t<T> inspect(T* v)
  13. {
  14. return inspect_t<T>(v);
  15. }
  16.  
  17. template<class C, class R, class T>
  18. basic_ostream<C,R>& operator << (basic_ostream<C,R>& out, const inspect_t<T>& t)
  19. {
  20. static const char *hexdigit = "0123456789ABCDEF";
  21. char *ptr = static_cast<char *>(static_cast<void *>(t.data));
  22. for(size_t i = 0; i < sizeof(T); i++)
  23. {
  24. int value = (static_cast<int>(ptr[i]))&0xFF;
  25. out << hexdigit[value/16] << hexdigit[value%16];
  26. }
  27. return out;
  28. }
  29.  
  30. struct Father
  31. {
  32. virtual void one() {}
  33. virtual void two() {}
  34. };
  35.  
  36. struct Mother
  37. {
  38. virtual void three() {}
  39. virtual void four() {}
  40. };
  41.  
  42. struct Child: public Father, public Mother
  43. {
  44. void five() {}
  45. void six() {}
  46. };
  47.  
  48. typedef int (*SimpleFP_t)();
  49. typedef void (Father::*FatherMFP_t)();
  50. typedef void (Mother::*MotherMFP_t)();
  51. typedef void (Child::*ChildMFP_t)();
  52.  
  53. int main()
  54. {
  55. cout << "Ordinary case:" << endl;
  56. int i = 0x12345678;
  57. cout << inspect(&i) << endl;
  58. double d = 3.141592653589793;
  59. cout << inspect(&d) << endl;
  60.  
  61. cout << "Simple pointer:" << endl;
  62. int *p = &i;
  63. cout << inspect(&p) << endl;
  64.  
  65. cout << "Simple function pointer:" << endl;
  66. SimpleFP_t fp = main;
  67. cout << inspect(&fp) << endl;
  68.  
  69. FatherMFP_t ffp;
  70. MotherMFP_t mfp;
  71. ChildMFP_t cfp;
  72. cout << "Parent's member function pointer:" << endl;
  73. ffp = &Father::one; cout << inspect(&ffp) << endl;
  74. ffp = &Father::two; cout << inspect(&ffp) << endl;
  75. mfp = &Mother::three; cout << inspect(&mfp) << endl;
  76. mfp = &Mother::four; cout << inspect(&mfp) << endl;
  77.  
  78. cout << "Child's member function pointer, in Child's view:" << endl;
  79. cfp = &Child::one; cout << inspect(&cfp) << endl;
  80. cfp = &Child::two; cout << inspect(&cfp) << endl;
  81. cfp = &Child::three; cout << inspect(&cfp) << endl;
  82. cfp = &Child::four; cout << inspect(&cfp) << endl;
  83. cfp = &Child::five; cout << inspect(&cfp) << endl;
  84. cfp = &Child::six; cout << inspect(&cfp) << endl;
  85.  
  86. cout << "Child's member function pointer, in Parent's view:" << endl;
  87. ffp = &Child::one; cout << inspect(&ffp) << endl;
  88. ffp = &Child::two; cout << inspect(&ffp) << endl;
  89. mfp = &Child::three; cout << inspect(&mfp) << endl;
  90. mfp = &Child::four; cout << inspect(&mfp) << endl;
  91.  
  92. return 0;
  93. }
  94.  
Success #stdin #stdout 0s 3344KB
stdin
Standard input is empty
stdout
Ordinary case:
78563412
182D4454FB210940
Simple pointer:
0077DDBF
Simple function pointer:
00860408
Parent's member function pointer:
0100000000000000
0500000000000000
0100000000000000
0500000000000000
Child's member function pointer, in Child's view:
0100000000000000
0500000000000000
0100000004000000
0500000004000000
708C040800000000
808C040800000000
Child's member function pointer, in Parent's view:
0100000000000000
0500000000000000
0100000000000000
0500000000000000