fork download
  1. #include <iostream>
  2.  
  3. class A
  4. {
  5. int& r_;
  6. public:
  7. A(int& v) : r_(v) {}
  8. int& get() { return r_; }
  9. void operator=(const A& a)
  10. {
  11. new(this)A(a.r_);
  12. }
  13. };
  14.  
  15. class B : public A
  16. {
  17. char c_;
  18. public:
  19. B(int& v, char c) : A(v), c_(c) {}
  20. char getch() const { return c_; }
  21. void operator=(const B& b)
  22. {
  23. A::operator=((A&)b);
  24. c_ = b.c_;
  25. }
  26. };
  27.  
  28. void test()
  29. {
  30. int x = 10;
  31. int y = 20;
  32. B d(x,'#');
  33. B e(y, '!');
  34. d = e;
  35. ++y;
  36. std::cout << "x=" << x << "\n"
  37. << "y=" << y << "\n";
  38. std::cout << "d.get()=" << d.get()
  39. << ", d.getch()=" << d.getch() << "\n"
  40. << "e.get()=" << e.get()
  41. << ", e.getch()=" << e.getch() << "\n";
  42. }
  43.  
  44. int main(int argc, char *argv[])
  45. {
  46. test();
  47. return 0;
  48. }
  49.  
Success #stdin #stdout 0s 3344KB
stdin
Standard input is empty
stdout
x=10
y=21
d.get()=21, d.getch()=!
e.get()=21, e.getch()=!