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:
&&
.