fork(1) download
  1. #include <functional>
  2. #include <string>
  3. #include <iostream>
  4.  
  5. struct Guy ;
  6.  
  7. struct Girl
  8. {
  9. Girl( Guy& g ) : boyfriend(g) {}
  10. std::reference_wrapper<Guy> boyfriend ;
  11. };
  12.  
  13. struct Guy
  14. {
  15. const std::string name ;
  16. // ...
  17. };
  18.  
  19. int main()
  20. {
  21. Guy a { "a" };
  22.  
  23. Girl b(a) ;
  24. std::cout << b.boyfriend.get().name << '\n' ;
  25.  
  26. Guy c { "c" } ;
  27. b.boyfriend = c ;
  28. std::cout << b.boyfriend.get().name << '\n' ;
  29.  
  30. Guy no_one { "no one" };
  31. b.boyfriend = no_one ;
  32. std::cout << b.boyfriend.get().name << '\n' ;
  33. }
  34.  
Success #stdin #stdout 0s 3028KB
stdin
Standard input is empty
stdout
a
c
no one