Summary of const keyword details

Preface

The keyword const is versatile. You can use it to modify the constants in the global or namespace scope outside the class, or to modify the objects declared as static in the file, function, or block scope, or to modify the inside of the class. The static and non-static member variables.
In the face of pointers, it can also point to the pointer itself, the pointer to the object, or both (or neither) are const.

const and pointer

规则:如果const出现在星号左边,表示被指物是常量;如果出现在星号右边,表示指针自身是常量;如果出现在星号两边,表示被指物和指针两者都是常量。

Modified pointer variable

If the referent is a constant (const appears on the left side of the asterisk), some habits will write const before the type, and some habits will write it after the type and before the asterisk. The two writing methods have the same meaning:

void f1(const Widget* pw);	//f1获得一个指针,指向一个常量的(不变的)Widget对象
void

Guess you like

Origin blog.csdn.net/XZ2585458279/article/details/114822973