0x01:const Constants
1 Const int Ten ; 2 int 1 2 3 4 5 6 7 8 9 Ten }; 3 sizeof (a) << Endl; // sizeof (a) is
The C + + compiler optimizes const, references constants, directly replaces constants, does not read memory, reads constant symbol tables
Const int Ten ; int 5 << num << endl; // Print results: cout << *p << Endl; // printing Results 5cin. Get (); // The C + + compiler optimizes const, references constants, directly replaces constants, does not read memory, reads constant symbol tables
Const variable, in memory, can be modified, cannot take effect, C language reads memory value, C + + does not read memory
0x02:const Array
1 Const inta[5] = {1,2,3,4,5 };2 3 for(int*p = const_cast<int*> (a); P<a +5; p++)4 {5*p + =1;6 }7 8 for(inti =0; I <5; i++)9 {Tencout << A[i] <<Endl; One } ACin.Get();
The result is:
Single constant, C + + non-read memory, read constant symbol table
constant Array, C + + read Memory
0x03:const parameters
1 void Const int x)// cannot assign a value to a constant, const as a parameter, indicating a permission problem, cannot be modified within a function 2{3 // x = ten; // cannot assign a value to a constant 4 }
Const as a parameter table permission problem, within the function can not be modified, to prevent you random play.
0x04:const references
1 void Print (constint & rnum)// refers to a variable that cannot be modified within a function
1 int - ; 2 3 Const int & rnum = num; // referenced data, cannot be modified by reference name 4 5 int Const& rnum = num; // If Const is on the right, the compiler ignores the & right const
0x05:const and Classes
1 //construct, destructor, can not add const modifier2 classMyClass3 {4 Public:5 intx;6 inty;7MyClass (): X (Ten), Y ( -)8 {9 Ten } One~MyClass () A { - - } the void Set() - { -x =101; -y =Ten+x; + } - int Get()Const + { A returnX +y; at } - }; - - voidMain () - { - ConstMyClass *p (NewMyClass); inP->Get();//Const object, can only read members, cannot be written as a member -}
The \const member can be initialized by the constructor if it is not initialized.
1 classMyClass2 {3 Public:4 intx;5 Const intY//A const member can be initialized by a constructor if it is not initialized, even if there is an initial value that can be initialized by the constructor function6MyClass (intAintB,): X (a), Y (b)7 {8 9 }Ten~MyClass () One { A - } - void Set() the { -x =101; -y =Ten+x; - } + int Get()Const - { + returnX +y; A } at - Private: - -};
Member of class is Const cannot be modified by member function
classmyclass{ Public: Const intx; Const inty; MyClass (): X (Ten), Y ( -) { } ~MyClass () {}void Set()//member of class is Const cannot be modified by member function{x=101; Y=Ten+x; }};
1MyClass My1 ( -, $);2cout << my1.x <<Endl;3 4 Const int*p = &my1.x;//Take variable address5 int*PX = const_cast<int*> (P);//Change Pointer6*PX =123;7 8cout << my1.x << Endl;//the member of the 123 class is a const constant that can be removed from the Const property modification
(1) A member of a class is a const
1.const without initialization, can be constructed to initialize, even if there is an initial value, can also be initialized by the constructor of the assigned value
2. A member of a class is const and cannot be changed by a member function and can be read
3. The member of the class is a const constant that can be removed from the Const property modification
(2) The member function of the class is const, the member variable of the class can not be modified, only read
(3) The member function parameter of the class is preceded by a const, the argument is read only and cannot be changed
(4) const If as a pointer to the return type modifier of a class member function parameter, the qualification can only be read by this pointer and cannot be written.
0x06:const pointer
Const on the left side of the *, limited to the data can not be changed, you can change the point
1 Const int *p (newint(5)); // Const on the left side of the *, limited to the data can not be changed, you can change the point 2 // *p = 3;const on the left side of the * number, limited to the data can not be changed 3 New int (ten); // can change the point
Const on the right, the pointer cannot be changed, can change the data pointed to by the pointer
int const P (newint(5)); // const on the right, the pointer cannot be changed, can change the data pointed to by the pointer 3; // You can change the data that the pointer points to // p = new int (ten); const on the right, the pointer cannot be changed
P is a constant pointer to a constant that points to the data and pointer can not be modified
1 int Const const P (newint(5)); 2 // p is a constant pointer to a constant that points to the data and pointer can not be modified
C + + Const detailed