Two keywords that modify C + + member functions summarize constant member functions (const keyword)
The Const modifier member function indicates that this member function cannot modify a member variable of a class, so this member function is called a constant member function. This allows the compiler to optimize the function in depth. In addition, a constant-type example of a class can only invoke a constant-type member function. Let's say the following example.
class Test{public: Test(); int getValue() const; int value();private: int intValue;};Test::Test():intValue(1){}int Test::getValue() const{ return intValue;}int Test::value(){ return intValue;}
The Class Test has GetValue () and value () Two member functions, one of which is a constant type. Then in the following code:
int main(){ Test A; const Test B; cout << A.getValue() << endl; cout << A.value() << endl; cout << A.getValue() << endl; cout << B.value() << endl; return 0;}
Access to B.value () is illegal because B is a constant and can only access constant-type member functions.
As a good habit, we should set all functions in the class that do not modify member variables to be const.
The const member function, however, does not definitely change the members of the class. For example, the following code:
#include <iostream> using namespace std; char STRING[] = "A STRING!";class Test{public: Test(); char * changeStr() const;private: char *str;};Test::Test(): str(STRING){}char * Test::changeStr() const{ str[1] = ‘x‘; return str;}int main(){ const Test A; cout << A.changeStr(); return 0;}
The const modifier can only guarantee that the address pointed to by the pointer str is not modified. The value of the string pointed to can be arbitrarily changed. It is not the same if you change str from char * to an array. The following code cannot be compiled. Because str[1] = ' x ' This operation is illegal.
#include <iostream>using namespace std;class Test{public: Test(); char * changeStr() const ;private: char str[10];};Test::Test(){ strcpy(str, "A STRING!");}char * Test::changeStr() const{ str[1] = ‘x‘; return str;}int main(){ Test A; cout << A.changeStr(); return 0;}
Volatile member functions
In contrast to const, volatile means that variables accessed in this function may change for other reasons. For example, other threads may assign values to these variables, or the hardware will have to write other mechanisms to change the values of these variables. Such a function should be modified with the volatile keyword, which is equivalent to telling the compiler that the value of the variable accessed in the function can change at any time, so do not make access optimizations. Here is an example where a is defined as a volatile variable and a.value () must be defined as volatile, otherwise it cannot be accessed.
#include <iostream>using namespace std;class Test{public: Test(); int value() volatile;private: int v;};Test::Test(){ v = 1;}int Test::value() volatile{ return v;}int main(){ volatile Test A; cout << A.value() << endl; return 0;}
Volatile and const can also modify a function at the same time. Indicates that the function itself does not modify the value of the variable, but the value of the variable may change itself. Here is an example:
#include <iostream>using namespace std;class Test{public: Test(); int value() volatile const;private: int v;};Test::Test(){ v = 1;}int Test::value() volatile const{ return v;}int main(){ volatile const Test A; cout << A.value() << endl; return 0;}
Two keyword summaries for modifying C + + member functions