#include<vector>
#include<string>
#include<iostream>
usingnamespace std;
int main() {
int v=1;
int &r=v;
int *pv=&v;
int *pr=&r;
cout<<pv<<*pv<<endl;
cout<<pr<<*pr<<endl;
return 0;
}
the results of the two output is totally, so it means reference's address is totally the same as its original? why, it do not need a place to store where it is representing??? you know, even a constant has a address.
a reference is not a label functioning like a literal expression when compiling, right?? But if it don't have an address, it loooks like so.
Short answer: no, they don't have their own address
More technical answer: they might have their own address in some circumstances, but you can't access that address in C++
Explanation:
Conceptually, a reference is just an alias for the referred variable. Therefore when you say: int &r=v;, that means that r "is" v. They're both different names to access the same variable.
Therefore since v and r are the same variable, &v would be equal to &r as your test shows.
How does C++ store a variable's name and value? does it store the object's name in one place and its value in another place? and then link them together? or the name is a label in the source code, and it always know where to find its value?
The program accesses those variables by reading/writing that memory location.
The user friendly names that we give our variables do not exist in the compiled program (except for things like debugging symbols, but that's another topic)