As @Igor Tandetnik indicated, foo.check returns a reference, but b is an int, not a reference to int, so it keeps the original value.
What you want can be achieved by ...
#include <iostream>
using namespace std;
class Foo
{
public:
int a = 1;
int &check()
{
return a;
};
};
int main()
{
Foo foo;
int &b { foo.check() };
cout << b << endl;
foo.check() = 2;
cout << foo.a << endl;
cout << b << endl;
return 0;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…