C/C++ const modifier usage summary

insert image description here

I am a rookie who has been playing C++ for three years, so some of the content of this article may be wrong, please forgive me.

Role: read-only, cannot be modified.

Rule: const acts on the thing to its left by default, otherwise acts on the thing to its right. Read from right to left.

const applies to the thing left of it. If there is nothing on the left then it applies to the thing right of it.

Variable declaration:

name statement explain meaning
constant integer const int a const modifies int to be a constant integer Value cannot be modified, must be initialized
constant integer int const a const modifies int to be a constant integer Value cannot be modified, must be initialized
variable pointer to constant integer const int *a const modifies int to be a constant integer, * acts on a constant integer The pointer can be modified, the content cannot be modified
variable pointer to constant integer int const *a const modifies int to be a constant integer, * acts on a constant integer The pointer can be modified, the content cannot be modified
constant pointer to variable integer int* const a const modifies * as a constant pointer, and int indicates that it is a variable integer The pointer cannot be modified, but the content can be modified
const pointer to const integer const int* const a The const on the left modifies int as a constant integer, and the const on the right modifies * as a constant pointer The pointer cannot be modified, the content cannot be modified
const pointer to const integer int const* const a The const on the left modifies int as a constant integer, and the const on the right modifies * as a constant pointer The pointer cannot be modified, the content cannot be modified
const pointer to const integer const int* a const The const on the left modifies int as a constant integer, and the const on the right modifies * as a constant pointer The pointer cannot be modified, the content cannot be modified
The variable pointer points to a constant pointer to a constant integer int const * const *a The const on the left modifies int as a constant integer, the const on the right modifies * as a constant pointer, and the * on the right acts on a variable, indicating that this is a variable pointer The pointer can be modified, the pointer of the pointer cannot be modified, and the pointer content of the pointer cannot be modified
const pointer to a const pointer to const integer int const * const * const a The const on the left modifies int as a constant integer, and the const on the middle and right modifies * as a constant pointer The pointer cannot be modified, the pointer of the pointer cannot be modified, and the pointer content of the pointer cannot be modified
constant reference const int &a const modifies int to be a constant integer The referenced value cannot be modified through a constant reference, it must be initialized
constant reference int const &a const modifies int to be a constant integer The referenced value cannot be modified through a constant reference, it must be initialized
reference constant int &const a does not exist does not exist
constant object const MyClass a const Modifies MyClass as a constant object You cannot modify the value of member variables, you can only call constant member functions

Function declaration:

name statement meaning
constants as function arguments void MyFun (const int a) It is not allowed to modify the value of the constant inside the function
const reference as function parameter void MyFun (const int &a) It is not allowed to modify the value of the constant inside the function
const member function void MyFun () const Constant member functions are not allowed to modify member variables, nor are they allowed to call non-constant member functions
functions that return constant values const int MyFun () Prevent the return value of the function from being used as an lvalue, and as an rvalue, only const int type can be assigned
function that returns a constant reference const int& MyFun () Prevents functions from returning values ​​as lvalues ​​and does not call the copy constructor
Functions that return constant pointers const int* MyFun () Prevent the return value of the function from being used as an lvalue, and as an rvalue, only const int pointers can be assigned
function returning a const pointer to a const integer const int* const MyFun () Prevent the function return value as an lvalue, and as an rvalue, it can assign values ​​to const int* const and const int pointers

Guess you like

Origin blog.csdn.net/baidu_39514357/article/details/129586976