Uncommon keywords in c ++

Source: Internet
Author: User

Some not commonly used keywords in some occasions very useful, reproduced from the http://www.softhouse.com.cn/news/show/13406.html

 

Mutable keyword

Mutable is an uncommon keyword in C ++, it can only be used for non-static and non-static data members of the class. We know that the status of an object is determined by the non-static data members of the object. As the data members change, the status of the object also changes!

If a member function of a class is declared as a const type, this function will not change the object state, that is, the function will not modify non-static data members of the class. however, in some cases, you need to assign values to the data members of the class in this type of function. the mutable keyword is required at this time.

For example:
Class Demo
{
Public:
Demo (){}
~ Demo (){}
Public:
Bool getFlag () const
{
M_nAccess ++;
Return m_bFlag;
}
Private:
Int m_nAccess;
Bool m_bFlag;
};

Int main ()
{
Return 0;
}

Compile the code above to see the error C2166: l-value specifies const object. This indicates that the non-static data member of the class is changed in the const type function. in this case, you need to use mutable to modify the non-static data member to be changed in the const member function.

M_nAccess, the Code is as follows:

Class Demo
{
Public:
Demo (){}
~ Demo (){}
Public:
Bool getFlag () const
{
M_nAccess ++;
Return m_bFlag;
}
Private:
Mutable int m_nAccess;
Bool m_bFlag;
};

Int main ()
{
Return 0;
}

In this way, no errors will occur during re-compilation!

Volatile keywords

Volatile is a little-known keyword in C/C ++ that tells the compiler not to hold a temporary copy of a variable. It can be applied to basic types.

For example, Int, Char, long... also applies to the structure of C and the class of C ++. When the structure or Class Object is modified using volatile, all the members of the structure or class are considered volatile. Using volatile does not contradict the need for synchronization objects such as critical_section, mutex, and event.

For example:
Int I;
I = I + 3;
In any case, I will always be placed in a register for a short period of time, because arithmetic operations can only be performed in registers. In general, the volatitle keyword applies to rows rather than rows.

Let's first implement a simple function to observe the deficiencies in the compilation code generated by the compiler, and to observe how the volatile keyword fixes the deficiency. There is a busy loop in this function (the so-called busy loop is also called Busy waits, which is a high waste of CPU time)

Void getkey (char * PCH)
{
While (* PCH = 0)
;
}

After you close all the optimization options in the VC development environment, compile the program and obtain the following results (assembly code)
; While (* PCH = 0)
$ L27
; Load the address stored in pch
Mov eax, dword ptr _ pch $ [ebp]
; Load the character into the EAX register
Movsx eax, byte ptr [eax]
; Compare the value to zero
Test eax, eax
; If not zero, exit loop
Jne $ L28
;
Jmp $ L27
$ L28
;}

This section of unoptimized code constantly loads the appropriate address, loads the content in the address, and tests the results. Efficiency is quite low, but the results are very accurate. Now let's take a look at all the compiler's optimization options after the switch is turned on, re-compile the program, the generated assembly code, and the above Code

What are the differences?
;{
; Load the address stored in pch
Mov eax, dword ptr _ pch $ [esp-4]
; Load the character into the AL register
Movsx al, byte ptr [eax]
; While (* pch = 0)
; Compare the value in the AL register to zero
Test al, al
; If still zero, try again
Je short $ l84
;
;}

The code length is much shorter than that without optimization. Note that the compiler places the mov command out of the loop. This is a very good Optimization in a single thread. However, in a multi-threaded application, if another thread changes the variable value, the loop will never end. The tested value is always placed in the register. Therefore, this section of code has a huge bug in the case of multithreading. Solution: Re-

Write the getkey function once and declare the PCH parameter as volatile. The Code is as follows:

Void getkey (volatile char * PCH)
{
While (* PCH = 0)
;
}

This modification has no impact on the non-optimized version. The following figure shows the results after optimization:

;{
; Load the address stored in PCH
MoV eax, dword ptr _ PCH $ [esp-4]
; While (* PCH = 0)
$ L84:
; Directly compare the value to zero
CMP byte PTR [eax], 0
; If still zero, try again
Je short $ l84
;
;}

The modification results are perfect and the address will not change. Therefore, the address Declaration is moved out of the loop. The address content is volatile, so it is constantly re-checked in each loop. It is legal to pass a const volatile variable as a parameter to the function. Such a declaration means that the function cannot change the value of the variable, but the value of the variable can be changed by another thread at any time.

Explicit keywords

When writing an application, the explicit keyword is rarely used. Its function is to "prohibit Single-parameter constructors" and is used for automatic type conversion, A typical example is the container type. In this type of constructor, you can pass the initial length as a parameter to the constructor.

For example:

You can declare such a constructor
Class Array
{
Public:
Explicit array (INT size );
......
};

Here, the explicit keyword plays a vital role. Without this keyword, this constructor can convert int to array. once this happens, you can give the array tribe an integer without causing any problems, such:

Array arr;
...
Arr = 40;

In this case, the automatic type conversion of C ++ converts 40 to an array with 40 elements and assigns it to the ARR variable. This result is not the expected result at all. if we declare the constructor as explicit, the assignment operation above will cause the compiler to report an error so that we can detect the error in time. it should be noted that explicit can also prevent "initialization with transformation operations using the value assignment Syntax ";

For example:
Array Arr (40); // correct
Array arr = 40; // Error

Take a look at the following two operations:

X x;
Y y (x); // explicit type conversion
Another
X x;
Y y = x; // implicit type conversion

There is a small difference between the two operations. The first method generates a new object of Type Y based on Type x through explicit type conversion; the second method generates a new object of Type Y through implicit conversion. the application of the explicit keyword is mainly the definition of the constructor mentioned above. You can refer to the application of this keyword to see the STL source code. This keyword is widely used.

_ Based keywords

This keyword is mainly used to solve some problems related to the shared memory. It allows the pointer to be defined as a 32-bit offset value starting from a certain point, rather than the absolute location of the memory type.

For example:

Typedef struct tagDEMOSTRUCT {
Int;
Char sz [10];
} DEMOSTRUCT, * PDEMOSTRUCT;

HANDLE hFileMapping = CreateFileMapping (...);
LPVOID lpShare = (LPDWORD) MapViewOfFile (...);

DEMOSTRUCT _ based (lpShare) * lpDemo;

The above example declares a pointer lpDemo, which internally stores the offset value starting from lpShare, that is, the lpHead is based on lpShare.

The DEMOSTRUCT in the preceding example is just a structure defined randomly to represent any structure.

Although the _ Based pointer is very easy to use, you must pay a certain cost for efficiency. every time you use the _ Based pointer to process data, the CPU must add a base address for it to point to the real location.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.