A reference is also called an alias. It can be the most object name, but it is neither an object pointer nor a copy of an object.
Objects can be indirectly operated through reference. The usage is similar to a pointer, but the pointer syntax is not required.
Rules:
1> when a reference is created, it must be initialized. (The pointer can be initialized at any time.
Int a = 12;
Int & B = a; // correct
Int & c; // Error
2> once the reference is initialized, the reference relationship cannot be changed (the pointer can change the object at any time)
3> all referenced operations are actually applied to the objects it refers to, including the addressing operators.
4> a null reference is not allowed. The reference must be associated with a valid storage unit (the pointer can be NULL)
Direct Access: Access variable values by variable address
Indirect access: access the variable through the variable of the variable address
Pointer: the pointer is the variable whose value represents the memory address.
If both pointers point to elements in the same array, they can be subtracted.
Any pointer can be compared to test whether they are equal or not.
Similarities and differences between pointers and references
Similarities:
It is the concept of address. The Pointer Points to a piece of memory. Its content refers to the address of memory, and the reference is the alias of a piece of memory.
Differences:
1> A pointer is an entity, and a reference is an alias.
2> the reference can only be initialized once during definition, and cannot be changed later. The pointer can. The reference is "from one end to the end", and the pointer is "thinking differently".
3> the reference has no const, the pointer has const, And the pointer with const cannot be changed.
4> the reference cannot be empty, but the pointer can be empty.
5> Sizeof references the size of the variable (object), while sizeof obtains the size of the pointer.
6> pointer and reference auto-increment (++) operations have different meanings
7> references are type-safe, but pointers are not (references have more type checks than pointers)
From: Feige's column