A common type is a type described by the Type modifier Const. The values of variables or objects of a common type cannot be updated. Therefore, Initialization is required to define or describe the common type.
Constants and object Constants
1. General Constants
A constant is a constant of a simple type. When defining such constants, the modifier const can be used before or after the type specifier. For example:
Int const x = 2;
Or
Const int x = 2;
Define or describe a regular array in the following format:
<Type description> const <array name> [<size>]…
Or
Const <type specifier> <array name> [<size>]…
For example:
Int const A [5] = {1, 2, 3, 4, 5 };
2. Regular objects
A constant object is an object constant. The definition format is as follows:
<Class Name> const <Object Name>
Or
Const <Class Name> <Object Name>
When defining a common object, Initialization is also required, and the object cannot be updated. The modifier const can be placed after the class name or before the class name.
Regular pointers and regular references
1. Constant pointer
When the const is used to modify the pointer, the position of the const is different, but the meaning is different. The following two examples illustrate their differences.
The following is a constant pointer to a string:
Char * const prt1 = stringprt1;
Ptr1 is a constant pointer. Therefore, the following assignment is invalid.
Ptr1 = stringprt2;
The following values are valid:
* Ptr1 = "M ";
Because the variables pointed to by the pointer ptr1 can be updated, the direction indicated by the constant pointer ptr1 cannot be updated (other strings ).
The following defines a pointer to a String constant:
Const * ptr2 = stringprt1;
Ptr2 is a pointer to a String constant. Ptr2 points to strings that cannot be updated, while ptr2 can be updated. Therefore,
* Ptr2 = "X ";
Is invalid, and:
Ptr2 = stringptr2;
It is legal.
Therefore, when using const to modify the pointer, pay attention to the position of Const. When defining a pointer constant pointing to a string and a pointer pointing to a String constant, the position of the const modifier is different. The former const is placed between * and the pointer name, the latter const is placed before the type specifier.
2. Frequent reference
The const modifier can also be used to describe the reference. The referenced object cannot be updated. The definition format is as follows:
Const <type specifier> & <reference Name>
For example:
Const double & V;
In practical applications, common pointers and common references are often used as function parameters. Such parameters are called common parameters.
In C ++ object-oriented programming, pointers and references are used a lot, and the constant pointers and references modified by const are used more. If you use a common parameter, this function does not update the object to which a parameter is directed or referenced. In this way, you do not need to copy the initialization constructor during parameter passing, this will improve the program running efficiency.
The following example describes how to use a regular pointer as a function parameter.
# Include Const int n = 6; Void print (const int * P, int N );Void main () { Int array [N]; For (INT I = 0; I cin> array [I]; Print (array, N ); } Void print (const int * P, int N) { Cout <"{" <* P; For (INT I = 1; I cout <"," <* (p + I ); Cout <"}" <}
|
Common member functions
A member function that uses the const keyword for description. Only regular member functions are eligible to operate constants or regular objects. member functions without the const keyword cannot be used to operate regular objects. Common member functions are described in the following format:
<Type description> <Function Name> (<parameter table>) const;
Here, const is the type modifier after the function description. It is an integral part of the function type. Therefore, the const keyword must also be included in the function implementation part. The following example describes the features of a common member function.
# Include Class R { Public: R (INT R1, int R2) {R1 = R1; r2 = R2 ;} Void print (); Void print () const; PRIVATE: Int R1, R2; };Void R: Print () { Cout <} Void R: Print () const { Cout <} Void main () { R a (5, 4 ); A. Print (); Const r B (20, 52 ); B. Print (); }
|
The output result of this example is:
5, 4
20; 52
The class of this program declares two member functions, whose types are different (in fact, they are reloaded member functions ). A member function with a const modifier processes const constants, which also reflects the feature of function overloading.
Regular data member
The Type modifier const not only describes member functions, but also data members.
Because the const type object must be initialized and cannot be updated, const data members can only be generated through the initialization list of Members when being described in the class.
The following example describes how to generate a constructor by using the member initialization list.
# Include Class { Public: A (int I ); Void print (); Const Int & R; PRIVATE: Const int; Static const int B; };Const int A: B = 10; A: A (int I): A (I), R () { } Void A: Print () { Cout <} Void main () { A A1 (100), A2 (0 ); A1.print (); A2.print (); }
|
The running result of this program is:
AM
The program describes the following three common data members:
Const Int & R;
Const int;
Static const int B;
Here, R is a reference of the common int type, A is a constant int type variable, and B is a static constant int type variable.
Initialize static data member B in the program.
It is worth noting that the constructor format is as follows:
A (int I): A (I), R ()
{
}
The colon is followed by a data member initialization list. It contains two initialization items separated by commas (,). Because data members A and R are of the common type, the initialization format must be used.