C++: const and constexpr keywords

1. const keyword, when reading the const keyword statement, read from right to left.

cosnt int* p; and int const * p; are equivalent. (Constant pointer): A pointer to a constant, the content pointed to by the pointer cannot be modified to a constant, and the pointer can be modified. For example const int* p = new int[10];

int* const p; (pointer constant): Pointer constant, the pointer itself is a constant, the pointer itself cannot modify the address it points to, and the content in the address can be changed.

const reference (reference constant): The value of the original variable cannot be modified through a reference constant, but the value of the original variable can be changed through the variable name. This approach prevents variable values ​​from being modified by passing applications.

 

2. constexpr keyword (text expression) (constant expression) (C++11)

Text expressions, which can be used as constants, the most typical example is:

const intgetArraySize() { return 32; }

int myArray[getArraySize()];

The text expression requires that the expression parameter is also a text parameter, and the return value type is also a text type.

Text constructor, without function body, all parameters must be initialized with parameters of text type.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325562875&siteId=291194637