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