C++const modified member function

Constant function:
1. After adding const after the member function, we call this function a constant function.
(In fact, adding const after the member function modifies the this point, so that the value pointed to by the pointer cannot be modified. The original this pointer is a pointer constant, and the pointer point cannot be modified. After adding const, the value cannot be modified. .)

2. It is not possible to modify member attributes in regular functions

3. After adding the keyword mutable to the member attribute declaration, it can still be modified in the regular function

Constant object:
1. Add const before the declared object to call the object a constant object
(the properties of the constant object cannot be modified, but if the declaration of the constant object's properties is added with mutable, then the mutable attribute can be modified)

2. Constant objects can only call constant functions
(constant objects cannot call ordinary member functions, because ordinary member functions can modify properties)

Guess you like

Origin blog.csdn.net/m0_51955470/article/details/113141168