c++ - Why does a const reference behave differently when assigned an l-value and r-value? -
given code sample below;
#include<iostream> using namespace std; int main(){ int = 4; const int &b = a; const int &c = * 2; = 10; cout << b << endl; cout << c << endl; return 0; } when run this, output
10 8 why c++ designed behave differently when assigning l-value , r-value const references?
the expression:
const int &c = * 2; does not bind resulting reference c a. instead binds rvalue result of expression a * 2, temporary object no longer has a - changing a not affect it.
this opposed b, reference object a.
Comments
Post a Comment