Introduction When I write a program to use const, or read other people's code and encounter const, I often stop and think for a while. Many programmers never use const because they did not even use Const. This article only discusses the usage of const, hoping to help improve the source code quality of the software. Constant Variables The variable is modified with const, and its value cannot be changed. Any code that changes this variable will produce a compilation error. Const can be added before and after the data type. For example: Void main (void) { Const int I = 10; // I, j are used as common variables Int const J = 20; I = 15; // error. The constant variable cannot be changed. J = 25; // error. The constant variable cannot be changed. } Regular pointer There are two methods for using const with pointers. Const can be used to limit the immutable pointer. That is to say, the memory address pointed to by the pointer is immutable, but the memory content pointed to by the address can be changed at will. Int main (void) { Int I = 10; Int * const J = & I; // constant pointer, pointing to int Type Variable (* J) ++; // you can change the variable content. J ++; // error, cannot change the memory address pointed to by the regular pointer } Const can also be used to limit that the memory to which the Pointer Points is unchangeable, but the memory address to which the Pointer Points is variable. Int main (void) { Int I = 20; Const int * j = & I; // pointer, pointing to an int type constant // You can also enter int const * j = & I; J ++; // The memory address pointed to by the pointer is variable (* J) ++; // error. The memory content cannot be changed. } After reading the two examples above, are you confused? In the first example, const is used to modify the pointer J and J is immutable (that is, the constant pointer pointing to the int variable). In the second example, const is used to modify * j, * j is immutable (that is, pointer to an int constant ). These two methods can be combined to make the pointer and memory content immutable. Int main (void) { Int I = 10; Const int * const J = & I; // a constant pointer to an int constant J ++; // error. The pointer address cannot be changed. (* J) ++; // error. The constant value cannot be changed. } Const and reference Reference is actually the alias of a variable. There are several rules: Variables must be initialized Once initialized, the reference cannot point to other variables. Any change to the reference will change the original variable. References and variables point to the same memory address. The following example demonstrates the above rules: Void main (void) { Int I = 10; // I and j are int variables. Int J = 20; Int & R = I; // R is a reference to variable I Int & S; // error. It must be initialized when a reference is declared. I = 15; // both I and R are equal to 15 I ++; // both I and R are equal to 16 R = 18; // both I and R are equal to 18 Printf ("Address of I = % u, address of R = % u", & I, & R); // The memory address is the same R = J; // both I and R are equal to 20, but R is not a reference of J. R ++; // both I and R are equal to 21, and J is still equal to 20 } Use const to modify the reference so that the application cannot be modified, but this does not delay the reference to reflect any modifications to the variable. Const can be added before and after the data type. For example: Void main (void) { Int I = 10; Int J = 100; Const Int & R = I; Int const & s = J; R = 20; // error, cannot change content S = 50; // error, cannot change content I = 15; // both I and R are equal to 15 J = 25; // both J and S are equal to 25 } Const and member functions When declaring a member function, add the const modifier at the end to indicate that no data of the object can be changed in the member function. This mode is often used to indicate the read-only access mode of object data. For example: Class myclass { Char * STR = "Hello, world "; Myclass () { // Void Constructor } ~ Myclass () { // Destructor } Char valueat (int pos) const // const method is an accessor method { If (Pos> = 12) Return 0; * STR = 'M'; // error. The object cannot be modified. Return STR [POS]; // return the value at position POS } } Const and overload You can also use const when reloading a function. Consider the following code: Class myclass { Char * STR = "Hello, world "; Myclass () { // Void Constructor } ~ Myclass () { // Destructor } Char valueat (int pos) const // const method is an accessor method { If (Pos> = 12) Return 0; Return STR [POS]; // return the value at position POS } Char & valueat (int pos) // set the memory content by returning the reference { If (Pos> = 12) Return NULL; Return STR [POS]; } } In the preceding example, valueat is overloaded. Const is actually a part of the function parameter. In the first member function, it restricts that this function cannot change the data of the object, but the second function does not. This example is only used to demonstrate that const can be used to reload functions. It has no practical significance. In fact, we need a new version of getvalue. If getvalue is used on the Right of operator =, it acts as a variable. If getvalue is used as the unary operator, the returned reference can be modified. This method is often used to overload operators. OPERATOR [] of the string class is a good example. (This paragraph is very bad. The original Article is as follows: In reality due to the beauty of references just the second definition of getvalue is actually required. if the getvalue method is used on the right side of an = operator then it will act as an accessor, while if it is used as an L-value (left hand side value) then the returned reference will be modified and the method will be used as setter. this is frequently done when overloading operators. the [] Operator in string classes is a good example .) Class myclass { Char * STR = "Hello, world "; Myclass () { // Void Constructor } ~ Myclass () { // Destructor } Char & operator [] (int pos) // you can change the memory content by returning a reference. { If (Pos> = 12) Return NULL; Return STR [POS]; } } Void main (void) { Myclass m; Char CH = m [0]; // ch equals to 'H' M [0] = 'M'; // The member str of M becomes: Mello, world } Const concerns In C/C ++, the default data transfer method is value transfer. That is to say, when a parameter is passed to the function, a copy of the parameter is generated, in this way, no changes to this parameter in this function will be extended beyond this function. Each time you call this function, a copy is generated, which is inefficient, especially when the number of function calls is high. For example: Class myclass { Public: Int X; Char valueat (int pos) const // const method is an accessor method { If (Pos> = 12) Return 0; Return STR [POS]; // return the value at position POS } Myclass () { // Void Constructor } ~ Myclass () { // Destructor } Myfunc (INT y) // pass the value { Y = 20; X = y; // both X and Y are equal to 20. } } Void main (void) { Myclass m; Int z = 10; M. myfunc (z ); Printf ("z = % d, myclass. x = % d", Z, M. X); // Z unchanged, X equals 20. } The above example shows that Z has not changed because myfunc () operates on the copy of Z. To improve efficiency, we can pass parameters by reference instead of passing values. In this way, the reference of this parameter is passed to the function, instead of copying this parameter. However, the problem is that if the parameter is changed inside the function, this change will be extended to the outside of the function and may cause errors. Add the const modifier before the parameter to ensure that the parameter is not changed within the function. Class myclass { Public: Int X; Myclass () { // Void Constructor } ~ Myclass () { // Destructor } Int myfunc (const Int & Y) // The reference is passed without any copy. { Y = 20; // error. The constant variable cannot be modified. X = y } } Void main (void) { Myclass m; Int z = 10; M. myfunc (z ); Printf ("z = % d, myclass. x = % d", Z, M. X); // Z is not changed, and X is equal to 10. } In this way, const uses this simple security mechanism to prevent you from writing code that may overhead your head and bite you. You should try to use const references. By declaring your function parameters as constant variables (wherever possible) or defining the const method, you can effectively establish the concept: this member function does not change any function parameters or data of this object. When other programmers use the member functions you provide, they will not worry about their data being altered. |