Yes, I know. But a reference is a kind of pointer, so it needs the address of r-value, too... |
This seems to be what is getting you.
References are not pointers.
1 2
|
int var = 100;
int& ref = var;
| |
ref does not "point to" var.
ref
is var. They're one and the same.
This is different both conceptually, and in how the compiler generates the code.
As for being able to point to string literals, C/C++ make an exception to the rule for that, simply because there's really no other way to handle hardcoded strings in a program.
Strings do not exist in the actual assembly. IE you can't do something like
mov eax, "a string literal"
. Other types of literals
do exist in the assembly, though.
mov eax, 5
for instance.
Therefore the compiler takes all the string literals in a program, slaps them somewhere else in the compiled binary, and let's you refer to them via pointer.
In theory, it
could do the same with other types so that you could point to other literals, but there wouldn't really be any point to that.