This article is mainly written to the C learner who understands the misunderstanding of Const grammar. I hope all ambiguous friend who is more blurred in this area can read and find some answers. . 2004-11-19 21:00 Const The earliest idea is to replace the pre-processor #define macro to form a constant concept. For constant const objects, Const pointers, and Const, the Const class member, and the const function return type, the Const class member, and the Const member function, and some summary of Const last understanding is described. 1 Const object and const type of Const types are described below for both concepts. INT-shaped content cannot be modified for 1,2 these two constations for objects, although the expression is different but the effect is the same. Because the change in the object itself puts the content to the object is the change of the content of the object, the latter is also changed to change the former. So the same semantic. 2 Const pointer and pointer to ConST and the combination of the two describes the following 1. INT * const p; / / pointer P is constant cannot be modified, for example, P ; // Modify P it error // modify P point to content * p = 2; // ok 2. const INT * p; // P is the content pointing to a pointer to a plastic constant cannot change P ; // ok // * p = 2; // error 3. Const int I * const p; // Pointer P itself cannot be modified and all valid content cannot be // modified * p = 2; error and p ; error 3 const parameter modification and parameter return type const modification 1.const parameters Modification of the specific usage of the function parameter modified Const 1 2 The method is the same, for example, Void Fun (const I) {i ;} // error cannot modify constant i 2. Const modified function return type usage is also similar to 12, Only modified object changes becomes a return object, for example: const Int fun () {static int i; return i;} int res = (fun ()) // error can not modify constant return object 4 Const class member and const Member Functions 1. Const member CONST members will allow initialization during construction and cannot be changed in the future. We can know that CONST members and general const variables are different, and the CONST member is meaningful for each object. Because he is initialized during the construction period, only the configuration will be constructed after the class is instantiated. So the CONST member can describe this: When each instantiation of the class is initialized, it cannot be changed in the living cycle of this object. 2. Const member function description: void class :: memberfun () const {}; // This const {}; all class member variables are not allowed to be modified after this function body is allowed. This will bring some benefits to prevent your unexpected processing. Summary: <1> Const constant General compiler does not assign space just to maintain a table. When External externally references this constant or "&", the compiler will assign an address for it. Const itself is more complicated. <2> Const Memory Law Const Modification The nearest name. When I was first studied, I was confused by constraints. Later, I will slowly summarize I think so is the easiest. Example: const Int i; at this time, Const only modify INT indicates that i is not a constant but i's content is constant.
Because the change in C / C expression to i is the change of the contents of the I, so i is also similar to a const. Everyone may wish to use the pointer const and try to understand what I want to help. <3> For all non-Const types, there is unconditional conversion to const types, but the latter cannot automatically convert to the former unless explicit forced transformation removes CONST. This makes sense, because the const type is a subset of non-Const is a special, which is generally transformed into special reasonable, like template, and inherited upward maps are meaningful. <4> Remembering all const modifications is not always inseparable, if the human forced conversion compiler will not be reminded. Because it is not obligated, we should be careful when we translate it. <5> When the Const class member function is processed, we introduced Mutable modified member variables, and the modified member variables via its modification can be modified in the Const class member function, the compiler is allowed. Other members that are not modified by Mutable or by Const rules cannot be changed in the Const member function. "end"