Frequent type refers to the type of type modifier constant, and the usual type variable or object value cannot be updated. Therefore, initialization must be performed when defined or explaining the normal type.
General constants and object constants
General constant
General constants refers to a simple type of constant. This constant is defined, and the modifier Const can be used before the type specifier can be used before the type specifier. Such as:
INT const x = 2;
or
Const int x = 2;
Define or explain that a constant group can be used in the following form:
or
Const
E.g:
INT const a [5] = {1, 2, 3, 4, 5};
2. Ordinary object
Often object refers to object constants, and the definition format is as follows:
or
Const
When defining a normal object, it is also to be initialized, and the object cannot be updated, and the modifier const can be placed behind the class name or in front of the class name.
Often pointer and often reference
Standing pointer
When using a const modifier, the content is different due to the location of the Const. Two examples will be given below to illustrate their difference.
A constant pointer to a string defined below:
Char * const prt1 = stringPRT1;
Where PTR1 is a constant pointer. Therefore, the following assignment is illegal.
Ptr1 = StringPRT2;
The following assignments are legal:
* ptr1 = "m";
Because the variable pointed to by the pointer PTR1 can be updated, the direction of the constant pointer PTR1 is not updated (other string).
The following defines a pointer to a string constant:
Const * Ptr2 = StringPRT1;
Where PTR2 is a pointer to a string constant. The string pointed to by PTR2 cannot be updated, and PTR2 can be updated. therefore,
* ptr2 = "x";
It is illegal, and:
PTR2 = StringPtr2;
It is legal.
So, when using a const modifier pointer, you should pay attention to the location of Const. Define a pointer constant to string and define a pointer to a string constant, the position of the Const modifier is different, the former const is placed between * and pointer names, the latter const is placed before the type specifier.
2. Frequently reference
Use const modifiers can also be described with reference to the references that are described as a common reference, and the object referenced by this reference cannot be updated. Its definition format is as follows:
Const
E.g:
Const Double & V;
In practical applications, the normally pointers and common references are often used as a function of function, such parameters are referred to as endless parameters.
In C object-oriented programming, poor use is used, where there is more use of constative pointers and common references. Using the frequent parameters indicated that the function does not update the objects pointed to or referenced in a parameter, so that the copy initialization constructor is not required during the parameter transfer, which will improve the running efficiency of the program.
The following example shows the method of function parameters for the normal pointer.
#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 << "}" <}
Member function
The member function described using the const keyword is called a common member function. Only when the member function is eligible to operate constant or constant object, the member function that does not use the Const keyword cannot be used to operate the normal object. Member function description format is as follows:
Where Const is a type modifier that is added to the function description, which is an component of the function type, so in the function implementation part also takes the const key. An example of the characteristics of the common member function will be given below.
#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 program of the program declares two member functions, which are different (in fact, the heavy-duty member function). There is a member function with a Const modifier to handle constants, which also reflects the characteristics of the function overload.
Common data member
Type modifier Const can not only explain the member function, but also document the data member.
Since the const type object must be initialized and cannot be updated, the Const data member can only be initialized by the member initialization list when the CONST data member is described in the class.
The following example describes the use of a member initialization list to generate a constructor.
#include
Class A
{
PUBLIC:
A (INT I);
Void print ();
Const Int & R;
Private:
Const Int A;
Static const Int B;
}
Const Int A :: b = 10;
A :: A (INT I): A (i), R (a)
{
}
Void A :: Print ()
{
Cout <}
void main ()
{
A A1 (100), A2 (0);
A1.Print ();
A2.Print ();
}
The operation result of the program is:
100: 10: 100
0: 10: 0
In this program, the following three normal type data members are explained:
Const Int & r;
Const Int A;
Static const Int B;
Where R is a normal INT type, A is a constant INT variable, B is a static constant INT variable.
Static data member B is initialized in the program.
It is worth noting that the format of the constructor is as follows:
A (INT I): A (i), R (a)
{
}
Among them, the colon is a list of data member initialization, which contains two initializes, separated by commas, because data members A and R are often type, need to be initialized.