Const modification in C ++

xiaoxiao2021-03-06  23

2004-11-19 21:00

Const earliest idea is to replace the pre-processor #define this 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.

123456

1 object of Const objects and const types

The description of these two concepts is as follows

1. Int const object; // Object is a const amount is not possible to be modified Object = 2; Error

2. const Int Object; // Object is the content he stored in the Const INT type cannot be modified.

For the object of 1, 2 for the object, 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 combine

Describe the three concepts as follows

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 be changed to p ; // ok

// * p = 2; // error

3. Const Int * const p; // Pointer P itself cannot be modified and all valid content cannot be

// Modify * p = 2; error and p ; error

3 Const parameter modification and parameter return type const modification

1.const parameter modification

At this time, the specific usage of the function parameter modified constant 1 2 is the same

For example, Void Fun (const INT i) {i ;} // error does not modify constant i

2. Const modification function return type usage is similar to 12, only modified object changes become a return object

For example: const Int fun () {static int i; returni i;}

Int res = (fun ()) // error cannot modify constant return object

4 Const class members and Const member functions

Const member

Class Const members will allow initialization during construction and cannot be changed later. 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 functions

Description: Void class :: memberfun () const {}; // This const {}; this constant THIS all class member variables are not allowed to be modified after this function body. This will bring some benefits to prevent your unexpected processing.

to sum up:

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

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

New Post(0)