In-depth understanding of const,mutable and volatile in C + +

Source: Internet
Author: User

I always think that const represents a constant, a constant is a value that cannot be modified, but does not understand the implementation of const, and even do not know the existence of mutable and volatile, recently in the book to see this part of the knowledge, so this article will be detailed analysis of these keywords.

Here are a few questions to consider first:

1. The difference between const int A and int const A.

2. The difference between const char* s and Char *const S.

3. Is there a problem with the following code? What should I do if I have a problem?

class a{private:    int  A;     Const int Fun ()    {        1;    }     int Const     {        2;    }};

4. The following code must be able to achieve the purpose? Why?

// Delay Function void int a) {    while (a--)    {    }} 

5. What is the result of running the following code?

Const int 1 ; int* j = (int*) (&i); 2 ;p rintf ("%08x%d%08x%d", &i, I, J, *j);

For answers to the above questions:

1. No difference, indicating that a is a const int variable, a cannot be modified, and a may be optimized by the compiler (no memory address).

2. The const char* s indicates that s is a pointer to a constant string that can point to a different constant string, such as

Const Char " ABC "  "xyz";
However, the contents of the string cannot be modified, and the following code is faulted:
s1[0]='C';

char* const S represents a constant for a string pointer. Points to a unique string pointer that cannot be subsequently modified, and the following code is faulted:

Char Const " ABC "  "xyz";

3. The const int fun () means that a function return value is a const int (in fact, a const int is meaningless), and this function is no different from a normal function. the int fun () const indicates that the function is a constant function, the function cannot modify the member variable (specified) in the class, and if it is important to modify the member variable in the class, the member variable can be defined as mutable, as follows:

class a{private:    int  A;     Const int Fun ()    {        1;    }     int Const     {        2;    }};

4. The code may be optimized, this function is an invalid loop, this kind of code will be optimized by the compiler, no lag effect after optimization. If you must achieve the delay effect, you can define a as volatile. Volatile means that this variable can be modified for a variety of reasons, telling the compiler not to optimize the code generated by this variable (this is generally the underlying, hardware, and so on, if you do not understand volatile, it can lead to unexpected cases of code).

5. This code if it is in debug mode may be prompted by the compiler error, because there is a cast to modify the value of a constant (the compiler's understanding is that the constant can not be modified in any way, so the higher-level compiler may error this code), But this code is no problem at the language level of the C language, and C can convert arbitrary pointers to the required pointers. This code is output in C + +:

1 2

The output address is the same, but the value is not the same.

Why is that? Supposedly the address, his value is the same, the reason or in the compiler's optimization (not the compiler's fault, const is a constant, he should be unable to be modified, since can not be modified, with constant substitution is not a problem). The compiler will replace all the places that use I with his values, similar to # define in the C language. Here, the cast is used to force the value of the memory where I is located, but this value changes without modifying the compiler-optimized code (the optimized code is not related to the I symbol), so there is the above output. How to modify it? Using Volatile,volatile will require the compiler not to optimize the constant, and since it is not optimized, I will use the I symbol instead of the constant value. As follows:

Const volatile int 1 ; int* j = (int*) (&i); 2 ;p rintf ("%08x%d%08x%d", &i, I, J, *j);

As can be understood, const is a C + + in order to replace the C language define generated by the keyword, const will leave some pits. So use mutable and volatile to fill these pits. If you do not understand these problems with const, you may encounter pits in the development process.

In-depth understanding of const,mutable and volatile in C + +

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.