The role of the const qualifier: 1. Define a const constant: const can change an object into a constant and cannot be modified. Therefore, Initialization is required during definition, for example, const int bufsize = 512; 2. You can modify the parameters, return values, and even definition bodies of a function. Forced protection of things modified by const can prevent unexpected changes and improve program robustness. A non-const variable is defined in the general global scope, which can be accessed throughout the program; // file_1.ccint counter; // defines a non-const variable // file_2.ccextern int counter; // use the counter variable + + counter in the file_1.cc file. If the const variable is declared in the global scope, it is a local variable that defines the object and only exists in that file, cannot be accessed by other files; you can specify the const variable as extern to access the const object throughout the program // file_1.ccextern const int counter = fcn (); // define the const variable and specify it as extern. counter can be used as the global variable // file_2.ccextern const int counter; // use the counter variable + + counter; = in the file_1.cc File ========================================================== ========================================================== ============== Const and # define are different: the C ++ language can use const to define constants or # define to define constants, but the former has more advantages than the latter: 1. const constants have data types, however, macro constants do not have data types, so the compiler can perform type security checks on the former and only replace the latter. 2. Some integrated debugging tools can debug const constants, however, macro constants cannot be debugged. ========================================================== ========================================================== ==================== In C ++, the member variable cannot be modified within the function body modified by const. However, in some cases, you need to change the member variables in the const function. This requires the member variable to be set to the mutable type, for example, class C {public: C (int I): m_Count (I) {} int incr () const // here, the member function incr is specified as the const type, so the variables in the function cannot be modified, that is, the m_Count variable cannot be changed. If m_Count can be changed, convert m_Count to mutable {return ++ m_Count;} private: mutable int m_Count; // here}