Insight 10.1.1.
When trying to pronounce complex looking data types in C++ it sometimes helps to read the backwards.
int& b; can be read as โb is a reference to an integerโ.
void foo(const string& s);
void foo(string s);
int a = 5;
int b = a;
b and copies the value a has to it. When reading that code, you should picture a memory diagram that looks like this:
a and b that each hold 5.b does not change the value stored in a. This is how things usually work in C++.
& to the data type, like int& b;. The & change the type of `b โit no longer is an โintโ, it is a โreference to an intโ.
int& b; can be read as โb is a reference to an integerโ.
int& b = a; should look like:
a that holds 5 and b that is a reference to a.b is an alias for a - it is another way to name the exact same storage that a names. If we change b, we change that value. Run this Codelens sample to see its animation of the situation:
a or b and both variables โseeโ the new value.
&&.