for basic declarations
1. const int r=100; The standard const variable declaration is initialized because the default internal connection must be initialized, scoped to this file, and the compiler is replaced with 100 directly at compile time after type checking
2. Extend const int r=100; Change the const to an outer join, which is used to expand to the global, the memory is allocated at compile time, and can be uninitialized, just as a declaration, which the compiler considers to be defined elsewhere in the program
3. const int r[]={1,2,3,4};
struct S {int a,b;};
Const S s[]={(1,2), (3.4)}; Both of these are constant collections, and the compiler allocates memory for them, so the values in them, such as int temp[r[2], cannot be used during compilation, such that the compiler reports that a constant expression cannot be found for a pointer
1. const int *r=&x; The declaration r is a pointer to an x of a constant, and r points to an object that cannot be modified, but he can point to a constant of any address
2. int const *r=&x; Completely equivalent to usage 1, without any distinction
3. int * Const r=&x; The declaration r is a constant pointer, and he points to x,r the pointer cannot be modified, but the contents of the address he points to can be modified
4. const INT * Const r=&x; Combined 1, 3 usage, R is a constant-type pointer to a constant for type checking
You can assign a non-const object to a pointer to a const, because sometimes we don't want to modify the value of its object from this pointer, but you can't assign a const object to a non-const pointer, because it might change the value of the object by this pointer. But there is also the legalization of making this operation, using type casts to change the const object through pointers:
const int r=100;
int * ptr = const_cast<int*> (&r); C + + standard, language use: int * ptr = (int*) &r; for character Arrays
such as char * name = "the"; Such statements, at compile time, can be passed, but "" "is a constant character array, any action to modify his can be compiled but will cause run-time errors, if we want to modify the character array, we will use char name[] =" "; This form. for Functions
1. void Fuction1 (const int R); A const value is passed here for the parameter, meaning that the variable initial value cannot be changed by the function
2. const int Fuction1 (int); The const value is returned here. Meaning refers to the return of the original function of the variable value can not be modified, but the function returned by the value of the variable is made a copy, can not be modified to have no meaning, it can be assigned to any const or non-const type variables, do not need to add this const keyword. But this is only for the internal type (because the intrinsic type returns a value that is definitely not a variable and is not used as a left value), and for the user-defined type, the return value is constant is very important, see clause 3 below.
3. Class CX; There is a constructor inside, declared as CX (int r =0)
CX Fuction1 () {return CX ();}
Const CX Fuction2 () {return CX ();}
If you have the custom class CX above, and the function Fuction1 () and Fuction2 (), we do the following:
Fuction1 () = CX (1); No problem, can be called as left value
Fuction2 () = CX (1); Compilation error, the const return value is prohibited from being invoked as a left value. Because the left value modifies the return value as a variable, the const declaration prohibits such modification.
4. Const pass and return of pointers in functions:
int F1 (const char * pstr); The use of the Const modifier as a pass-through ensures that the initial value of the pass parameter is not modified through this pointer, and that any attempt to modify the *pstr within the function can cause a compilation error.
const char * F2 (); The meaning is that the pointer that the function returns to is a const object that must be assigned to a pointer that is also pointing to a const object.
const char * const F3 (); A const more than the above, the meaning of this const is only valid when he is used as a left value, it shows that the pointer in addition to the Const object, it can not be modified itself, so it can not be treated as a left value.
5. Const pass referenced in function:
void F1 (const x& px); Such a const reference pass is exactly the same as the most common function passed by value, he prohibits all modifications to the referenced object, except that passing by value creates a copy of the class object first and then passes through, and it passes the address directly, so this transfer is more efficient than passing by value.
* * Another only reference Const pass can pass a temporary object , because the temporary object is a const attribute, and is not visible, he has a short time in a local domain, so cannot use the pointer, only the reference to the const pass can catch this guy. for Class
1. First, for const member variables, the initialization member list can only be initialized in the constructor, and attempting to initialize the const member variable in the constructor body can cause a compilation error. Initialize the member list in the form of:
X:: X (int ir): R (IR) {}//suppose R is a const member variable of class X
2. Const member function. The first thing to mention about this concept is the Const object, just as a built-in type can define a const object (const int r=10;), and a user-defined type can also define a const object (const X px);), The compiler must ensure that the object cannot be changed within its lifecycle. If you define such a const object, then for all calls to the object that are not const member functions, the compiler will prohibit and error errors during compilation in order to guarantee the const attribute of the object. So if you want your member function to be able to operate on a const object, declare the function as a const member function. If f () is a member function in a class, its declarations are as follows:
int f () const; The const is placed at the end of the function, and the compiler checks the function, and any attempt to change the member variable and invoke the non-const member function in this function is considered illegal
The construct and destructor of a * * class cannot be a const function.
The
3. establishes a const member function, but still wants to use this function to change the data inside the object. Such a requirement will also be met frequently, especially in a harsh interview with the examiner. First of all, we have to understand the requirements of the examiner, because there are two ways to achieve, if the examiner asked not to change the original class of anything, just let you start from the current const member function, then you only use the aforementioned type cast method. The example is as follows:
//If there is a class called X, it has an INT member variable r, we need to ++r this r with a const member function f (), the code is as follows
void X::f () const
{ (const_ Cast<x*> (this))-> ++r; &NBSP}//The type cast through the this pointer implements
Another method is to use the keyword: mutable . If your member variable is defined this way:
mutable int R;
Then it tells the compiler that this member variable can be changed by a const member function. The compiler would not bother to examine him again.