fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <iostream>
  4. #include <string.h>
  5. using namespace std;
  6. struct B{
  7. const char * str;
  8. void bar(){cout << str << endl;};
  9. B(){str = "B";}
  10. };
  11. struct D1 : public virtual B{
  12. const char * str;
  13. void bar(){cout << str << endl;}
  14. D1(){
  15. str = "D1";
  16. }
  17. };
  18. struct D2 : public virtual B{
  19. const char * str;
  20. void bar(){cout << str <<endl;};
  21. D2(){
  22. str = "D2";
  23. }
  24. };
  25. struct C : public D1, public D2{
  26. const char * str;
  27. void bar(){cout << str << endl;};
  28. C(){
  29. str = "C";
  30. }
  31.  
  32. };
  33.  
  34. void hexDump(void *ptr,size_t size){
  35. printf("%d bytes at %p: ",size,ptr);
  36. unsigned char *p = (unsigned char*) ptr;
  37. for(size_t i =size-1;i!=0;i--){
  38. printf("%02X",p[i]);
  39. }
  40. printf("%02X",p[0]);
  41. printf("\n");
  42. }
  43. template<class T>
  44. void *forceCast(T ptr){
  45. return *((void**)&ptr);
  46. };
  47.  
  48. int main(){
  49. int i;
  50. int *p = &i;
  51. void(B::*pB)() = &B::bar;
  52. void(D1::*pD1)() = &D1::bar;
  53. void(D2::*pD2)() = &D2::bar;
  54. void(C::*pC)() = &D2::bar;
  55.  
  56. printf("\nint *p size: %u\n",sizeof(p));
  57. hexDump(&p,sizeof(p));
  58. printf("printf: %p\n",p);
  59. cout << "cout forceCast: "<< forceCast(p) <<endl;
  60.  
  61. printf("\nint *pB size: %u\n",sizeof(pB));
  62. hexDump(&pB,sizeof(pB));
  63. printf("printf: %p\n",pB);
  64. cout << "cout forceCast: "<< forceCast(pB) <<endl;
  65.  
  66. printf("\nvoid(D1::*pD1)() size: %u\n",sizeof(pD1));
  67. hexDump(&pD1,sizeof(pD1));
  68. printf("printf: %p\n",pD1);
  69. cout << "cout forceCast: "<< forceCast(pD1) <<endl;
  70.  
  71. printf("\nvoid(C::*pC)() size: %u\n",sizeof(pC));
  72. hexDump(&pC,sizeof(pC));
  73. printf("printf: %p\n",pC);
  74. cout << "cout forceCast: "<< forceCast(pC) <<endl;
  75. //hexDump(&pvC,sizeof(pvC));
  76.  
  77. C *c = new C();
  78. (c->*pB)();
  79. (c->*pD1)();
  80. (c->*pC)();
  81.  
  82.  
  83. printf("\n\nreplacing pC's content with (void*) pC\n");
  84. void* ptr = forceCast(pC);
  85.  
  86. memset(&pC,0,sizeof(pC));
  87. memcpy(&pC,&ptr,sizeof(ptr));
  88.  
  89. printf("\nvoid(C::*pC)() size: %u\n",sizeof(pC));
  90. hexDump(&pC,sizeof(pC));
  91. printf("printf: %p\n",pC);
  92. cout << "cout forceCast: "<< forceCast(pC) <<endl;
  93. (c->*pC)();
  94. //system("pause");
  95. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
int *p size: 4
4 bytes at 0xbf8cb614:  BF8CB610
printf: 0xbf8cb610
cout forceCast: 0xbf8cb610

int *pB size: 8
8 bytes at 0xbf8cb618:  0000000008048FB0
printf: 0x8048fb0
cout forceCast: 0x8048fb0

void(D1::*pD1)() size: 8
8 bytes at 0xbf8cb620:  0000000008048EF0
printf: 0x8048ef0
cout forceCast: 0x8048ef0

void(C::*pC)() size: 8
8 bytes at 0xbf8cb628:  0000000808048E30
printf: 0x8048e30
cout forceCast: 0x8048e30
B
D1
D2


replacing pC's content with (void*) pC

void(C::*pC)() size: 8
8 bytes at 0xbf8cb628:  0000000008048E30
printf: 0x8048e30
cout forceCast: 0x8048e30
D1