Basic C ++ skills: master the const, volatile, and mutable keywords.

Source: Internet
Author: User
Tags types of functions
In the C ++ programming process, const can be used frequently.
It plays an immeasurable role in ensuring program security.
It is the most accurate expression in one sentence: "Small soldiers make great contributions ".
With const, mutable cannot be used.
However, as a fellow brother of const, volatile disappears from the perspective of many people.
How small is volatile's responsibility?
Naturally, they are used in a variety of ways and are smart, so that new users are confused for a long time,
The following is a summary of the system discussion:
I. general applications
1. Const modifies the usage of various variables.
A. Replace define
# Define d_int 100
# Define d_long 100.29
.........
Const int d_int = 100;
Const d_int = 100; // If the int type is defined, int can be omitted.
Const long d_long = 100.29;
.........
Const Int & amp; A = 100;
Const replaces define. Although it increases the allocation space, it ensures type security.
In the C standard, the data defined by const is equivalent to the global data, and C ++ depends on the declared position.
.
B. Modify the variables related to pointers.
With three simple definitions:
Group1:
Int A = 0;
Const int * 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 modifying the reference, const is ignored.
Const Int & Const B = A; ----- [4]
Summary:
1. If const is on the left side of the asterisk, const is used to modify the variable pointed to by the pointer,
That is, the Pointer Points to immutable.
2. If const is on the right side of the asterisk, const is to modify the pointer itself, that is, the pointer itself is
Immutable.
Therefore, [1] and [2] are the same, and the content pointed to by the pointer is immutable (the const is placed in the Variable
The location of the declaration Operator is irrelevant ),
In this case, content cannot be changed, for example, * A = 3;
3. in [3], the pointer itself is immutable, and the content pointed to by the pointer is variable. In this case
The pointer itself cannot be
Make changes. For example, a ++ is incorrect.
4. In [4], the pointer itself and the content pointed to are constants. (Reference Special: Reference is added when you use
In case of meaning, add the variable it represents. So qualifiers on reference are
Ignoredv.
Extension:
Note:
1. Const Int & reference = 1000;
2. char * P = "const"
Char * & Q;

2. Various Applications of const in the function environment
Examples of common methods:
Const A & _ fun (const A & _ In); // modify the referenced input parameter
// A _ fun (const A & _ In );
// A & _ fun (const A & _ In );
// There are special steps in the above two types of functions, which are not detailed here... ..

Const A * _ fun (const A * _ In); // modifies pointer input parameters
Void _ fun () const; // modifies the class member function
Const A & _ fun (A & _ In); // modify the return value
Const A & operator (const A & _ In); // modify both input parameters and return values
A. Modify parameters
For example, void _ fun (const A * _ In) or void _ fun (const A & _ In );
After they are modified, the behavior characteristics during function execution are the same as described above,
Note: This does not change whether the original data is a const attribute.
B. Modify the function return value
Const A & _ fun ()
Const A * _ fun ();
Note: The pointer or reference of a local variable cannot be returned because of the non-synchronization of the life cycle.
(Except static ).
In addition, different meanings are shown as follows...
For a & return type, if you associate it with other variables,
Then, it actually executes the variable
(Or reference) indicates that the data is assigned. If you assign other values to it,
Then it is assigned a variable or a reference
Use the representative data, while const A & generally prevents it from being assigned a value as the left value.
There are a lot of details in this place (such as processing the returned temporary object in the continuous assignment,
Heavy-duty const and non-cosnt operators). In practice, You Need To summarize them.
Ii. Difficulties
3. Modify the const of the class member function.
Format: void _ fun () const {};
You need to know the following rules:
A. the const object can only access the const member function, but not the const object can access any
Including const member functions.
B. Members of the const object cannot be modified. However, objects maintained by the const object through pointers are
Yes.
C. The const member function cannot modify the data of an object, regardless of whether the object has the const nature.
It is in
During compilation, check whether the member data is modified.
E. However, the mutable modifier is used by any means in any situation.
Can be modified. Naturally, the const member function can be modified at this time...
4. Talk about volatile and "Full const object"
A volatile-modified class only allows access to a subset of its interfaces.
The implementer can only use const_cast to access all interfaces of this type.
,
Like const, the volatile attribute of the class is passed to its members.
Such as, its member variables cannot be modified, and the objects or native variables maintained by the pointer are
Modify. So we think: if the object maintains a char *, it is equivalent to char *
Const chrptr; instead of const char * cosnt chrptr; you need
This modifier prevents it or the resources it maintains: cosnt x * xptr; instead of x * const xptr;
Because the default behavior of the cosnt-modified object is the continuation variable: x * cosnt xptr;
More importantly, the volatile modified data,
The compiler cannot optimize its execution period stored in registers.
This feature is required for multi-threaded synchronization. For more information, see Andrei's GP series.
.
5. About the const_cast conversion Operator
The most basic usage of this keyword is to remove the const nature of the data.
It is worth noting that it only applies to pointers, references, and other directed types.
Refer:
1. Const semantics in Objective C ++
2. Andrei Alexandrescu volatile-a good helper for multi-thread programming

Related Article

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.