References and Pointers to C++ Primer annotations

  • 2020-07-21 09:31:51
  • OfStack


Reference (reference)

Reference: Refers to the lvalue reference (lvalue reference)

Reference: Takes a nickname to bind the object, rather than copying the initial value to the reference


Cannot be bound to literals and computations

A reference is not an object

All operations on references are performed on the object to which they are bound

With two exceptions, the type of reference and the object to which it is bound are strictly matched


int i = 1024;
int &r = i; // Square root  int Type of r, To refer to int Type of i 

double dval = 3.14;
int &reval = dval; //  x  int Type of reval, Can't quote double Type of dval

: star: pointer (pointer)

A pointer is an object that allows copying and assignment

Pointer definition: used to point to (store address). Pointers can only point to addresses

A pointer can point to a pointer of the same type


Define a pointer
Address of pointer itself:
& point
The address to which the pointer points
Access to what the pointer points to:



The & # 8195; Pointers can point successively to different objects of the same type

The & # 8195; Within the block scope, the defined pointer is not initialized and has an indeterminate value

The four states of the value of the pointer (address/point)

The & # 8195; 1. Point to an object

The & # 8195; 2. Points to the next position of an adjacent object

The & # 8195; 3. Null pointer, not pointing to any object

The & # 8195; 4. Invalid pointer, no other value satisfying the above three cases

Copying and accessing invalid Pointers cause errors that the compiler cannot detect


Related articles: