fork download
  1. #include <iostream>
  2. struct A;
  3. struct B
  4. {
  5. A& ref;
  6. int n;
  7. B(A& a) : ref(a), n(1) {}
  8. void show() const;
  9. };
  10.  
  11. struct A
  12. {
  13. B& ref;
  14. int n;
  15. A(B& b) : ref(b), n(2) {}
  16. void show() const;
  17. };
  18.  
  19. void A::show() const
  20. {
  21. std::cout << "A.n is " << n
  22. << " A.ref.n is " << ref.n
  23. << " A.ref.ref.n is " << ref.ref.n
  24. << " A.ref.ref.ref.n is " << ref.ref.ref.n
  25. << '\n';
  26. }
  27.  
  28. void B::show() const
  29. {
  30. std::cout << "B.n is " << n
  31. << " B.ref.n is " << ref.n
  32. << " B.ref.ref.n is " << ref.ref.n
  33. << " B.ref.ref.ref.n is " << ref.ref.ref.n
  34. << '\n';
  35. }
  36.  
  37. int main()
  38. {
  39. // std::pair<A, B> pair(pair.second, pair.first);
  40. struct {B first; A second;} pair = {pair.second, pair.first};
  41. pair.first.show();
  42. pair.second.show();
  43. }
  44.  
Success #stdin #stdout 0.02s 2724KB
stdin
Standard input is empty
stdout
B.n is 1 B.ref.n is 2 B.ref.ref.n is 1 B.ref.ref.ref.n is 2
A.n is 2 A.ref.n is 1 A.ref.ref.n is 2 A.ref.ref.ref.n is 1