The problem is that
&
is overloaded to mean different things, depending on context.
In your first example (line 4) it is the “get the address of” operator, returning a pointer to the rhs argument.
In your second example (line 1) it is not an operator at all, but part of the argument
type — the argument is a
reference to int (or
int reference).
The best way to think of a reference is as an
alias for another thing. So, given:
1 2
|
int x = 7;
int& y = x;
| |
You now have two names for a
single integer object. You can use either name to access the (singular) integer object.
This way of thinking is a bit unique, especially for those coming from the value-vs-pointer thinking from before. When using (C++) references, don’t treat them like pointers. They are not.
The underlying implementation may actually use pointers, if necessary, but that does not (and should not) matter to you.
Hope this helps.
[edit]
I think you might benefit from reading something I was considering for the FAQ from ages ago (that I have not had time to mess with for years). I am not entirely happy with the current presentation, but you can read the two pages anyway. I hope they are of value to you.
https://michael-thomas-greer.com/faq/values-pointers-references/ (part 1)
https://michael-thomas-greer.com/faq/pass-by/ (part 2)
C++ is a weird language.
Good thinking!