One has two questions:
- one syntax question: difference between pointer and link
- the other concerns mechanics and implementation: the representation in memory of a reference
Let me address the two separately.
Link and Pointer Syntax
A pointer conceptually means a “sign” (like a road sign) to an object. It allows you to perform 2 types of actions:
- actions on pointee (or the object pointed to)
- pointer action
operator* and operator-> allow you to access the pointer to distinguish it from your calls to the pointer itself.
The link is not a "sign", it is an alias. Throughout his life, hell or high water will come, he will point to the same object, and you can do nothing about it. Therefore, since you cannot access the link itself, there is no point bothering you with the strange syntax * or -> . Ironically, without using a strange syntax, it is called syntactic sugar.
Link mechanics
The C ++ standard does not talk about implementing links, it just hints that if the compiler succeeds in removing them. For example, in the following case:
int main() { int a = 0; int& b = a; b = 1; return b; }
A good compiler will understand that b is just a proxy for a , there is no room for doubt and, therefore, simply accesses a directly and optimizes b out.
As you may have guessed, the likely representation of a link is (under the hood) a pointer, but do not let it bother you, it does not affect the syntax or semantics. This means, however, that a number of pointer problems (for example, access to objects that have been deleted, for example) also affect links.
source share