fork download
  1. #include <iostream>
  2. #include <stdio.h>
  3. using namespace std;
  4.  
  5. struct vtbltest
  6. {
  7. virtual int testvirt()
  8. {
  9. return 666;
  10. }
  11. };
  12.  
  13. template <class T>
  14. struct clearable
  15. {
  16. void *cleared;
  17. clearable():cleared(calloc(sizeof(T), 1)) {}
  18.  
  19. void clear()
  20. {
  21. *((T*)this) = *((T*)cleared);
  22. };
  23. };
  24.  
  25. class test : public clearable<test>, public vtbltest
  26. {
  27. public:
  28. int a;
  29. };
  30.  
  31. class test2 : public clearable<test>, public vtbltest
  32. {
  33. public:
  34. int testvirt()
  35. {
  36. return 976;
  37. }
  38.  
  39. int a;
  40. };
  41.  
  42. int main() {
  43. test _test;
  44. _test.a=3;
  45. _test.clear();
  46. printf("%d, %d\n", _test.a, _test.testvirt());
  47.  
  48. test2 _test2;
  49. _test2.a=3;
  50. _test2.clear();
  51. printf("%d, %d\n", _test2.a, _test2.testvirt());
  52.  
  53. return 0;
  54. }
  55.  
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
0, 666
0, 976