fork(2) 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 Child : public Parent{
  18. public:
  19. Child operator=(const Child&) = delete;
  20. Child(const Child&) = delete;
  21. Child(const Parent* parent){
  22. const auto vTablePtrSize = sizeof(void*);
  23.  
  24. memcpy(reinterpret_cast<char*>(this) + vTablePtrSize,
  25. reinterpret_cast<const char*>(parent) + vTablePtrSize,
  26. sizeof(Parent) - vTablePtrSize);
  27. }
  28.  
  29. virtual void func(){ cout << "Child, parent says: "; Parent::func(); }
  30. };
  31.  
  32. int main() {
  33. Parent foo(13);
  34. Child bar(&foo);
  35.  
  36. bar.func();
  37.  
  38. return 0;
  39. }
Success #stdin #stdout 0s 3140KB
stdin
Standard input is empty
stdout
Child, parent says: 13