const, the pointer reference

premise

We ignore the same type (modifiers are the same) whether the case can be assigned (I still have to learn where you can also assign each other), and the type of compatible cases. Only consider const, &, *, etc. impact modifiers brought (originally issued an article, but there is no comprehensive point around, so rewrite)

Type compatibility:

  • Cast
  • Compatibility between the base class and subclass
  • Compatible class type constructor (the constructor parameter type may be converted to the type of class, converting only once)

Const front and rear cconst

const:

  • const initialization of variables must be bound to a value (variable or constant will do)
  • This binding value can be implicitly or display, most of the time display with safer
  • eg: string and other containers, using the constructor may be an implicit default initialization, const string str; it is legal

Front const:
Front const writing in front of other modifiers, that is far from the local variables

  • Such modifications const variables pointing to objects in general can be a variable can also be a constant
  • Can not modify the object pointed to
  • Nor can the variable assigned to the same type of non-const modified variables

Rear const:

  • With such modifications const variable, the variable will become constants
  • Is a right constant value, the operation can not be assigned

const pointer

pointer:

  • It will have its own space
  • Pointing to the target object

const type * ptr

this ptr consider himself points to a constant value can not be changed within the meaning of the object, but you can still freed memory, which is a variable ptr (left value), it can be assigned, but it can not be assigned to None const pointer of the same type of modification

type * const ptr

the ptr is constant (right value), does not accept assignment, ptr now has become a constant const

const reference

(Const without modification) Quote:

  • A value must be bound left
  • Take another alias
  • It can only be initiated once bound

variate of the type & temp = const

variate can be constants, variables may be, can not change the temp

(const type & temp can bind the right values, but the values are not necessarily the right type of const, may make the right temp lose its original value after such a bind Some of the features)

type & const temp = variate

should be given, from left to see the temp, const description temp is constant and can not be assigned, & explain to bind a left value, both self-contradictory

Guess you like

Origin www.cnblogs.com/Liberavi/p/11767357.html