const specifies the semantic constraint of “not modifiable”. It can modify various variables to be constants; when modifying a pointer, appearing on the left side of * indicates that the pointed object is constant, while appearing on the right side indicates that the pointer itself is constant. However, const is more commonly used in function declarations:
1. Returning a constant from a function
This practice often reduces some erroneous operations by users, such as (a*b) = c. Although it is generally known that this is meaningless, it cannot be ruled out that someone might write it this way. If you modify the return value of the operator* function to be const, you can avoid this error.
2. Modifying member functions
First, const modifies member functions to indicate that this function cannot modify the object’s content. There are two interpretations: one is bitwise constness: a member function can only be called const if it does not modify any bit of the object. However, in practice, many member functions do not meet this requirement but still pass the compiler’s bitwise test, as shown in the following image:

The operator[] theoretically cannot modify text, but it can be done through pointers:

Through this operation, hello world in t was changed to jello world. Therefore, there is another explanation for logical constness: a const member function can modify some internal data of the object as long as the client cannot see it. To modify some internal data of the object while passing the compiler’s bitwise test, the mutable keyword must be used to modify the internal data to be changed.
3. How to avoid code duplication between const and non-const member functions
We often write a const version function and a non-const version function for the same functionality, which leads to high code duplication between the two functions. Therefore, we will use the non-const version function to call the const version function to avoid this issue. This requires some conversion:

By using static_cast to convert the this pointer to const, and then calling the const type operator[]. Finally, the const of the return value is removed using const_cast. It is important to note that do not use the reverse approach—calling a non-const function from a const function, as you do not know what the non-const function has done and whether it will modify the object’s data.