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

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -