Const is a common type modifier in C ++. A common type is a type indicated by a const. The values of variables or objects of a common type cannot be updated.
1. Define Constants
(1) const modifies variables. The following two definitions are essentially the same. It means that the value of the TYPE variable modified by const is unchangeable.
TYPE const ValueName = value;
Const TYPE ValueName = value;
(2) Change const to an external connection to expand to the global level. during compilation, memory is allocated and Initialization is not allowed. It is only used as a declaration. The Compiler considers it defined elsewhere in the program.
Extend const int ValueName = value;
2. Use CONST for pointers
(1) the pointer itself is a constant that is unchangeable.
(Char *) const pContent;
Const (char *) pContent;
(2) The Pointer Points to a constant that is unchangeable.
Const (char) * pContent;
(Char) const * pContent;
(3) Both are unchangeable.
Const char * const pContent;
(4) There are also different methods. Draw a line along the "*" sign:
If the const is on the left side of *, const is used to modify the variable pointed to by the pointer, that is, the pointer points to a constant;
If const is on the right side of *, const is to modify the pointer itself, that is, the pointer itself is a constant.
3. Use CONST in Functions
(1) const modifier function parameters
A. the passed parameters cannot be changed in the function (meaningless, because Var itself is a form parameter)
Void function (const int Var );
B. The content indicated by the parameter pointer is constant immutable.
Void function (const char * Var );
C. The parameter pointer itself is a constant and immutable (also meaningless, because char * Var is also a form parameter)
Void function (char * const Var );
D. The parameter is a reference. In order to increase efficiency and prevent modification. When modifying referenced parameters:
Void function (const Class & Var); // The referenced parameter cannot be changed in the function.
Void function (const TYPE & Var); // The referenced parameter is a constant in the function and cannot be changed.
2) const modifier function return value
The const modifier function does not actually return a lot of values. Its meaning is basically the same as that of the const modifier for common variables and pointers.
A. const int fun1 () // This is actually meaningless, because the return value of the parameter itself is a value.
B. const int * fun2 () // const int * pValue = fun2 () when calling ();
// We can regard fun2 () as a variable, that is, the pointer content is immutable.
C.int * const fun3 () // int * const pValue = fun2 () when calling ();
// We can regard fun2 () as a variable, that is, the pointer itself is immutable.
4. class-related CONST
(1) const modifies member variables
A member function of the const modifier class, which indicates a member constant and cannot be modified. It can only be assigned a value in the initialization list.
Class
{
...
Const int nValue; // The member constant cannot be modified.
...
A (int x): nValue (x) {}; // values can only be assigned to the initialization list.
}
(2) const modifier member functions
If a const modifies a member function of a class, the member function cannot modify any non-const member function of the class. It is generally written at the end of the function.
Class
{
...
Void function () const; // constant member function, which does not change the member variable of the object.
// You cannot call any non-const member functions in the class.
}
(3) const modifier Class Object/Object Pointer/Object Reference
• The const modifier Class Object indicates that the object is a constant object, and no member can be modified. The same is true for object pointers and object references.
• Any non-const member functions of the const object cannot be called because any non-const member function will attempt to modify the member variables.
For example:
Copy codeThe Code is as follows: class AAA
{
Void func1 ();
Void func2 () const;
}
Const AAA aObj;
AObj. func1 (); // ×
AObj. func2 (); // correct
Const AAA * aObj = new AAA ();
AObj-> func1 (); // ×
AObj-> func2 (); // correct
5. Convert the Const type into a non-Const type Method
C ++ provides four conversion operators:
• Const case <new type> (expression)
• Static_cast <new_type> (expression)
• Reinterpret_cast <new_type> (expression)
• Dynamic_cast <new_type> (expression)
Use const_cast for non-Const type conversion.
Usage: const_cast <type_id> (expression)
This operator is used to modify the const or volatile attributes of the type. In addition to const or volatile modification, type_id and expression are of the same type.
• Constant pointers are converted to non-constant pointers and still point to the original object;
• Constant reference is converted to a non-constant reference and still points to the original object;
• Constant objects are converted to very large objects.
Copy codeThe Code is as follows: const int constant = 21;
Const int * const_p = & constant;
Int * modifier = const_cast <int *> (const_p );
* Modifier = 7;
Of course, we can use the following traditional method instead:Copy codeThe Code is as follows: const int constant = 21;
Int * modifier = (int *) (& constant );
As we can see in the previous code, we cannot modify constant, but we can re-assign values to modifier.
But is the program world really messy? Have we actually modified the constatn value through modifier? Is it true that C ++ has gone to the const to modify the data of the const variable?
If we print the result:
Copy codeThe Code is as follows: cout <"constant:" <constant <endl;
Cout <"const_p:" <* const_p <endl;
Cout <"modifier:" <* modifier <endl;
/**
Constant: 21
Const_p: 7
Modifier: 7
**/
The original value of constant is retained.
But they do point to the same address:
Copy codeThe Code is as follows: cout <"constant:" <& constant <endl;
Cout <"const_p:" <const_p <endl;
Cout <"modifier:" <modifier <endl;
/**
Constant: 0x7fff5fbff72c
Const_p: 0x7fff5fbff72c
Modifier: 0x7fff5fbff72c
**/
Although the value of const can be re-assigned, do not re-assign the value to the const data.