CONST

xiaoxiao2021-03-06  42

CONST

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 1. 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; definition or description A constant group can be used in the following format: Const [] ... or Const < Array name> [] ... for example: int covest a [5] = {1, 2, 3, 4, 5}; 2. Ordinary object constant object refers to object constants, defined formats as follows: Const or const When the normal object is defined, 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 pointers and often citations 1. When the constitutive pointer is used, the content is different due to the difference between the Const. Two examples will be given below to illustrate their difference. A constant pointer to a string defined below: char * const prinGprt1; where PTR1 is a constant pointer. Therefore, the following assignment is illegal. Ptr1 = StringPRT2; the following assignment is legal: * ptr1 = "m"; because the variable pointed to by the pointer PTR1 is updated, the direction of the constant pointer PTR1 is referred to (other string). The following defines a pointer to a string constant: const * ptr2 = StringPRT1; where PTR2 is a pointer to the string constant. The string pointed to by PTR2 cannot be updated, and PTR2 can be updated. Therefore, * PTR2 = "x"; 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 referenced using const modifiers can also explain the reference, the referenced references are often referenced, and the objects referenced by this reference cannot be updated. Its definition format is as follows: Const & Const Double & V; In practical applications, the normally pointers and common references are often used as a function of the function, such parameters are referred to as end 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 > Array [I]; Print (Array, N);} Void Print (const INT * P, INT N) {cout << "{" << * p; for (int i = 1; i () Const; where const is an integral part of the function type, it is a component of the function type, so The function implementation part also takes the const keyword. 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 << R1 << "," << R2 << Endl;} void r :: print () const {cout << r1 << "; << R2 << end1 } 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 class declares that two member functions 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. Often Data Members Type Modifier Const can also explain the member function, but also document data. 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 constinary;}; const INT A :: b = 10; a :: A (INT I): A (i), R (a) {} void A :: print () {cout << a << ":" << b << ":" << r << ENDL Void main () {A1 (100), A2 (0); A1.Print (); A2.Print ();} The program's operation results are: 100: 10: 100 0: 10: 0 In the program, the following three normal type data members are described: const INT & R; const Int A; static constinary; where R is a constant INT type, A is a constant INT variable, B is a static INT variable . Static data member B is initialized in the program. It is worth noting that the format of the constructor is shown below: A (INT I): A (i), R (a) {} where the colon is a data member initialization list, which contains two initializations, carry with commas Separated, because data members A and R are often type, need to be initialized. Bai Temen Technology Network 2004-9-11 16:51:25

Resources come from the Internet, or infringe on your rights, please contact us, we will delete within 24 hours.

转载请注明原文地址:https://www.9cbs.com/read-54519.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.048, SQL: 9