fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. struct X {
  6. explicit X(int n) : N(n) {}
  7. int N;
  8. void print() const { cout << "X::N=" << N << endl; }
  9. };
  10.  
  11. class Y {
  12. public:
  13. Y() {
  14. _v.push_back(X(10));
  15. _v.push_back(X(20));
  16. }
  17.  
  18. X& get(int index) {
  19. return _v.at(index);
  20. }
  21.  
  22. private:
  23. vector<X> _v;
  24. };
  25.  
  26. int main() {
  27. Y y;
  28. X& x1 = y.get(0);
  29. x1.print(); // prints 10
  30.  
  31. x1 = X(100);
  32. y.get(0).print(); // prints 100
  33. }
  34.  
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
X::N=10
X::N=100