fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. struct Guy ;
  5.  
  6. struct Girl
  7. {
  8. std::vector<Guy*> boyfriends ;
  9. };
  10.  
  11. struct Guy
  12. {
  13. Guy( const std::string& n ) : name(n) {}
  14. const std::string name ;
  15. // ...
  16. };
  17.  
  18. int main()
  19. {
  20. Guy a( "a" ), b( "b" ), c( "c" ) ;
  21.  
  22. Girl g ;
  23.  
  24. g.boyfriends.push_back( &a ) ;
  25. g.boyfriends.push_back( &b ) ;
  26.  
  27. for( std::size_t i = 0 ; i < g.boyfriends.size() ; ++i )
  28. std::cout << g.boyfriends[i]->name << ' ' ;
  29. std::cout << '\n' ;
  30.  
  31. g.boyfriends.push_back( &c ) ;
  32.  
  33. for( std::size_t i = 0 ; i < g.boyfriends.size() ; ++i )
  34. std::cout << g.boyfriends[i]->name << ' ' ;
  35. std::cout << '\n' ;
  36. }
  37.  
Success #stdin #stdout 0s 2988KB
stdin
Standard input is empty
stdout
a b 
a b c