If I want a pointer to point to another variable I make it do so by giving the address to the variable with &.
int foo = 10;
int *bar = &foo;
Now If I follow the same logic as above and instead creates a reference type.
int foo = 10;
int &bar = &foo;
I would think this should work, but it doesn’t. Why?
Because a pointer and a reference are not the same thing.
You can think of a reference as just meaning “another name for” or “alias”.
In other words,
baris just another name forfooin your example.When you do
int &bar = foo;, you wantbarto be another name forfoo, you don’t want to assign the address offootobar. However, pointers store the address of the object it points at, hence with a pointer you need the address-of operator to get the address offooand assign that to thebarpointer.