fork(2) download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. class Myclass {
  5. public:
  6. Myclass() : a(1.0) {}
  7.  
  8. float a;
  9. float &a_ref = a;
  10.  
  11. void addOne() {
  12. a = a + 1.0;
  13. }
  14. };
  15.  
  16. int main() {
  17. Myclass instance1;
  18. instance1.addOne();
  19.  
  20. //prints 2:
  21. std::cout << "instance.a_ref is " << instance1.a_ref << std::endl;
  22.  
  23. std::vector<Myclass> vec;
  24. Myclass instance2;
  25. vec.push_back(instance2);
  26.  
  27. vec.at(0).addOne();
  28.  
  29. //prints 1;
  30. std::cout << "vec.at(0).a_ref is " << vec.at(0).a_ref << std::endl;
  31.  
  32. std::cout << "vec.at(0).a_ref points to " << &vec.at(0).a_ref << std::endl;
  33. std::cout << "instance2.a lives at " << &instance2.a << std::endl;
  34. return 0;
  35. }
Success #stdin #stdout 0s 3272KB
stdin
Standard input is empty
stdout
instance.a_ref is 2
vec.at(0).a_ref is 1
vec.at(0).a_ref points to 0xbfca9cac
instance2.a lives at 0xbfca9cac