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