#include <iostream>
#include <vector>
class Myclass {
public:
Myclass() : a(1.0) {}
float a;
float &a_ref = a;
void addOne() {
a = a + 1.0;
}
};
int main() {
Myclass instance1;
instance1.addOne();
//prints 2:
std::cout << "instance.a_ref is " << instance1.a_ref << std::endl;
std::vector<Myclass> vec;
Myclass instance2;
vec.push_back(instance2);
vec.at(0).addOne();
//prints 1;
std::cout << "vec.at(0).a_ref is " << vec.at(0).a_ref << std::endl;
std::cout << "vec.at(0).a_ref points to " << &vec.at(0).a_ref << std::endl;
std::cout << "instance2.a lives at " << &instance2.a << std::endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgoKY2xhc3MgTXljbGFzcyB7CnB1YmxpYzogCiAgTXljbGFzcygpIDogYSgxLjApIHt9CgogIGZsb2F0IGE7CiAgZmxvYXQgJmFfcmVmID0gYTsKCiAgdm9pZCBhZGRPbmUoKSB7CiAgICBhID0gYSArIDEuMDsKICB9Cn07CgppbnQgbWFpbigpIHsKICBNeWNsYXNzIGluc3RhbmNlMTsKICBpbnN0YW5jZTEuYWRkT25lKCk7CgogIC8vcHJpbnRzIDI6CiAgc3RkOjpjb3V0IDw8ICJpbnN0YW5jZS5hX3JlZiBpcyAiIDw8IGluc3RhbmNlMS5hX3JlZiA8PCBzdGQ6OmVuZGw7CgogIHN0ZDo6dmVjdG9yPE15Y2xhc3M+IHZlYzsKICBNeWNsYXNzIGluc3RhbmNlMjsKICB2ZWMucHVzaF9iYWNrKGluc3RhbmNlMik7CgogIHZlYy5hdCgwKS5hZGRPbmUoKTsKCiAgLy9wcmludHMgMTsKICBzdGQ6OmNvdXQgPDwgInZlYy5hdCgwKS5hX3JlZiBpcyAiIDw8IHZlYy5hdCgwKS5hX3JlZiA8PCBzdGQ6OmVuZGw7CiAgCiAgc3RkOjpjb3V0IDw8ICJ2ZWMuYXQoMCkuYV9yZWYgcG9pbnRzIHRvICIgPDwgJnZlYy5hdCgwKS5hX3JlZiA8PCBzdGQ6OmVuZGw7CiAgc3RkOjpjb3V0IDw8ICJpbnN0YW5jZTIuYSBsaXZlcyBhdCAiIDw8ICZpbnN0YW5jZTIuYSA8PCBzdGQ6OmVuZGw7CiAgcmV0dXJuIDA7Cn0=