C ++: const reference, references, const pointer, pointer constant


Online statement:

Code name meaning
const int &r
int const &r
Constant reference to
reference to const
The binding constant references
int &const r Constant references The statement, there is no error code runs
const int *p
int const *p
Const pointer
pointer to const
A pointer constant
int *const p Pointer constant
const pointer
The pointer itself is constant

C ++ Primer:

Code name meaning
const int &r
int const &r
Constant reference to
reference to const
Const reference to
int &const r Constant references The statement, there is no error code runs
const int *p
int const *p
Pointing constant pointer
pointer to const
A pointer constant
int *const p Const pointer
const pointer
The pointer itself is constant

to sum up

  1. Constant reference provision can not modify the object referenced by its binding, but can modify the object in other ways.
    int i = 1;
    const int &r = i; //r绑定i,其后对r的操作实际上是针对i进行的
    r = 2;            //错误,不能通过修改r来实现对i的修改
    i = 2;            //i可修改
  1. There is no reference constant. Because the reference can only be bound to an object (left value) at initialization, after not rebind other objects, operations are actually a reference to an object reference bound to carry out the operation, that is, the binding relationship has not changed modified by const reference itself meaningless.
    int i = 1;
    int &const r = i;

Runtime Error

  1. const int & r is equivalent to int const & r, const int * p is equivalent to int const * p. General procedures will be converted into const int int const, const int therefore more commonly used.
    int i = 1;
    const int &r1 = i;
    int const &r2 = i;//r2的类型:const int &
    const int *p1 = &i;
    int const *p2 = &i;//p2的类型:const int *
  1. Distinguish between const int * p and int * const p, critical look at const. Before const *, const modifies described type, a pointer to the constant. After const *, const modification described is a pointer, the pointer itself is constant.
    int i = 1;
    const int *p1 = &i; //const修饰int,则p1指向const int
    int *const p2 = &i; //const修饰p2,则p2是常量
  1. Const pointer different online and book version. Recommend unless necessary, const int * p called constant pointer pointing to the int * const p itself is called pointer constant. Although such trouble spots, but the meaning is wrong, it will not be confused.
    int i = 1;
    const int *p1 = &i; //指向常量的指针
    int *const p1 = &i; //本身是常量的指针

Personal Common name:

Code name
const int &r
int const &r
Binding constants reference, a reference to the constant, constant reference
reference to const
int &const r Constant reference does not exist
const int *p
int const *p
Pointing constant pointer
pointer to const
int *const p Itself const pointer
const pointer
const int *const p Are constants referents itself and a pointer
int *&r Binding pointer reference, pointer references
const pointer

Reference blog: sort out the C ++ constant pointer and pointer constant for all this (which very detailed, worth a visit)

Published 77 original articles · won praise 25 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_34801642/article/details/104677031