fork download
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. class Parent{
  6. public:
  7. Parent operator=(const Parent&) = delete;
  8. Parent(const Parent&) = delete;
  9. Parent() = default;
  10. Parent(int complex) : test(complex) {}
  11.  
  12. virtual void func(){ cout << test; }
  13. private:
  14. int test = 0;
  15. };
  16.  
  17. class Parent2{
  18. public:
  19. Parent2 operator=(const Parent2&) = delete;
  20. Parent2(const Parent2&) = delete;
  21. Parent2() = default;
  22. Parent2(int complex) : test2(complex) {}
  23.  
  24. virtual void func2(){ cout << test2; }
  25. private:
  26. int test2 = 0;
  27. };
  28.  
  29. class Child : public Parent, public Parent2{
  30. public:
  31. Child operator=(const Child&) = delete;
  32. Child(const Child&) = delete;
  33. Child(const char* parent, const char* parent2){
  34. const auto vTablePtrSize = sizeof(void*);
  35. const auto parentSize = sizeof(Parent);
  36. auto charPtrThis = reinterpret_cast<char*>(this);
  37.  
  38. memcpy(charPtrThis + vTablePtrSize,
  39. parent + vTablePtrSize,
  40. parentSize - vTablePtrSize);
  41.  
  42. memcpy(charPtrThis + parentSize + vTablePtrSize,
  43. parent2 + vTablePtrSize,
  44. sizeof(Parent2) - vTablePtrSize);
  45. }
  46.  
  47. virtual void func(){ cout << "Child, parent says: "; Parent::func(); }
  48. virtual void func2(){ cout << "Child, parent2 says: "; Parent2::func2(); }
  49. };
  50.  
  51.  
  52.  
  53. int main() {
  54. Parent foo(13);
  55. Parent2 foo2(42);
  56. Child bar(reinterpret_cast<char*>(&foo), reinterpret_cast<char*>(&foo2));
  57.  
  58. bar.func();
  59. bar.func2();
  60.  
  61. return 0;
  62. }
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
Child, parent says: 13Child, parent2 says: 42