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
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.