This is also a cliché question, write this is actually want to take advantage of the summer vacation blog Rich.
I casually in Google, degrees Niang, sick soft engine search can get all kinds of explanations of reference, nothing more than "reference is different from the pointer, reference is a variable alias " " use the reference is to use the variable itself " "and so on these, so a lot of concept bombing," reference does not occupy the space "this statement seems to have been everywhere, spread very wide, almost flooding, has been submerged really think of the trend." So in the spirit of chasing the truth, the reference does not occupy the space of the matter explained clearly, as to the reference and the pointer is not a different thing, this philosophical question, we do not explore, really want to understand or ask C + + 's father.
Really need to figure out, you have to start from the code itself.
int Main () { int a=; int* b=&A; A=+; *b=;}
What's the point of looking at this piece of code first? Does that mean nothing? Well, it really doesn't make sense, and it's completely out of the surface, of course, if you see what's coming, then you have to look at nothing!
Well, to know what this code does, but also from the Assembly, after all, from the bottom of the view can be as much as possible to bypass the compiler, show its real behavior, is a good way to learn the basic concept. Because I am using the Linux platform, so I prefer to use the AT-T assembly display, if you are not familiar with, it is completely okay, hurriedly shut down the web, Hemp slip people! Hero stop, I'm here to play a joke with you lively atmosphere, eh, eh, that ye, can not copy the stool?
SUBL $ -,%esp//Allocate SpaceCall ___main MOVL $Ten,8(%ESP)//put 10 into the space represented by variable aLeal8(%ESP),%eax//Store the address of variable A in the register EAXMOVL%eax, A(%ESP)//The address of variable A is stored in another address, where Pointer bMOVL $ -,8(%ESP)//assign 30 to variable aMovl A(%ESP),%eax//assigns the address of variable A to the register EAXMOVL $ -, (%EAX)//assigns a value of 20 to the address represented by the value of the register eax,
By commenting (it should be clear), you can completely correspond to the behavior of the above code, where you can truly feel the certainty of its behavior from the skin and position. As you can see, everything is under our control.
Here's a little bit of code modification:
int Main () { int a=; int& b=A; A=+; b = C;}
By contrast, you can see that the pointer is simply replaced by a reference, well, semantically, its behavior seems to have found some change, but what happens inside? Please go down:
Subl $,%ESP call ___main movl $8(%ESP) Leal 8(%ESP),%eax movl (%ESP) MOVL $ 8(%ESP) movl (%ESP),%eax movl $ (%EAX)
なに! What has changed in the code? Did you see the change coming? If you look out, go out and turn right at the Big pharmacy eye drops for two drops.
Can see at the assembly level, its behavior does not have any change, and the pointer is identical, let us stop, do not think, the pointer to occupy 4 bytes of space, you reference since and the pointer is the same thing, why do not occupy? Well, it's just a matter of fact.