C ++ basic work: Comprehensively master const, volatile and mutable keywords

zhaozj2021-02-16  50

C basic work: Comprehensively master const, volatile, and mutable keyword C program design, Const's usage can be very high. It has played an incapacitated role in ensuring program security. Use a sentence to express the most exact "Smart". With constings, Mutable is certainly can't. However, as a compatriots of Const, Volatile disappears in many people's field of view. In fact, Volatile is responsible for a small? Nature, their usage Diverse and smart, in a long time for a long time, let's take a look at the system.

I. General application 1. Const modification of various variables. A. Replaces Define #define D_INT 100 #define d_long 100.29 ......... Const Int D_INT = 100; const D_INT = 100; // If the defined int type, omitable INT. const line d_long = 100.29; ......... const INT & A = 100; Const replacement Define Although it increases allocation space, it guarantees type security. In C standard, the data defined by const is equivalent to global, while C The location of the declaration is determined. B. The modified pointer-related variables are denoted by three sets of simple definitions: group1: int a = 0; const * b = & a; ------------ [1] INT const * b = & a; ------------ [2] const INT * const b = & a; ---- [4] group2: const char * p = "const"; ------------ [1] char const * p = "const"; -------------- [2] char * const p = "const"; -------------- [3] const char * const p = "const"; ---- [4] group3: int a = 0; const Int & b = a; --- ------------ [1] Int const & b = a; --------------- [2] int & const b = a; ---- ---------- [3] // ---> When modified, Const is ignored Const Int & Const B = A; ----- [4] Summary: 1. If Const is located in Star No left side, CONS T is used to modify the variable pointed to by the pointer, that is, the pointer is not variable. 2. If the const is on the right side of the asterisk, the const is the modified pointer itself, that is, the pointer itself is not changed. Therefore, [1] and [ 2] The same is the same, the pointer points to the content of the pointer are unality (constant is not related to the location of the variable declaration), which is not allowed to make changes to the content, such as * a = 3; 3. [3] It is not variable, and the content points to the pointer is variable. In this case, the pointer itself cannot be changed, such as the A is an error 4. [4] The pointer itself and the point to the content are constant. Quote Special: Reference When using increasing sense, add the variable representative. So Qualifiers on Reference Are Ignoredv. Extension: Note Example: 1.const Int & Reference = 1000; 2.char * p = "const" char * & q; 2. Const a & _fun (const A & _IN); const a & _fun;

// Modified reference incoming parameter // a _fun (const a & _in); // The two of the above, there are special steps inside the function, which is not detailed here ... .. Const a * _fun (const a * _in); // Decorative Reflecting Pin-type Parameter VoID _Fun () const; // Repair Class member function const a & _fun (a & _in); // Modify return value const a & operator A & _IN); / / Simultaneously modify the incoming parameters and return values ​​a. Modified parameters such as void _fun (const a * _in) or void _fun (const A & _IN); after they are modified, the behavior characteristics during the function execution Explanation, note: This will not change whether the original data is constant.

b. Modified function return value const a & _fun () const a * _fun (); Note: The pointer of the local variable cannot be returned due to the problem of the life phase, except for STITIC. In addition, the viewing situation Represents Different Means ... For A & Return Type, if you assign it with other variables, then it actually assumes the data represented by the returned variable (or reference) .. and you give it other values, Then the variable is given or the data representative. And Const A & is generally prevented as the left value being assigned.

There is still a lot of details in this place (for example, in a continuous assignment, the returned temporary object, the overloaded const and non-COSNT operators, etc.), the reader needs more summary in practice.

Second, difficult to point 3. Const. Shaped like: void _fun () const {}; you need to know some rules:

A.Const object can only access the Const member function, not the const object to access any member function, including the Const member function. The member of the Const object is unmodified, but the Const object is maintained by the pointer, but it can be modified. The. C.const member function does not modify the data of the object, regardless of whether the object has a const nature. It is based on whether it is compiled, and checks. E. E. E. In any case, you can modify it through any means. Naturally, the Const member function at this time can modify it ...

4. Talk about Volatile and "Full Const Object" A class with Volatile modified class only allows access to a subset of its interface, which is controlled by the implementation of the class. The user can only access this type of all interface only by const_cast. Moreover, like Const, the class's Volatile property will pass to its member. Imagine the Constly modified object, its member variable is unmodified, and it can be modified by the object or native variable maintained by the pointer. Then Think: If the object maintains a char *, it is equivalent to Char * const chrptr; not const char * cosnt chrptr; for the pointer in the class, you need to modify it to prevent it or it maintained resources: cosnt x * xptr; Not x * const xptr; because COSNT modified object it default behavior is a continuation variable: x * cosnt xptr;

More importantly, Volatile modified data, the compiler cannot perform the optimization of the register in the execution period. This feature is for multi-threaded synchronization. Interests are visiting the ANDREI's GP series articles.

5. Talking about const_cast conversion operators This keyword is: Remove the constant nature of the data. It is worth noting that it is only the type of pointer, reference, and other types of pointing properties.

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

New Post(0)