The const usage of liunx programming-Linux general technology-Linux programming and kernel information. The following is a detailed description.
1. const Basics
If the const keyword does not involve pointers, we can understand it very well. The following is a case involving pointers:
Int B = 500;
Const int * a = & B; [1]
Int const * a = & B; [2]
Int * const a = & B; [3]
Const int * const a = & B; [4]
If you can differentiate the above four situations, congratulations! you have taken a great step. I don't know. It doesn't matter. We can refer to the practice on Item21 in Objective c ++. 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 a constant. If the const is on the right side of the asterisk, the const modifies the pointer itself, that is, the pointer itself is a constant. Therefore, [1] is the same as [2], and the content pointed to by the pointer is a constant (The position of the const in the variable declaration is irrelevant ), in this case, the content cannot be changed, for example, * a = 3; [3] indicates that the pointer itself is a constant, and the content pointed to by the pointer is not a constant, in this case, the pointer itself cannot be changed. For example, a ++ is incorrect. [4] indicates that the pointer itself and the content pointed to are constants.
In addition, some powerful functions of const are their application in function declaration. In a function declaration, const can modify the return value of a function or a parameter. For a member function, it can also be modified as a whole function. In the following situations, we will gradually describe the usage:
A & operator = (const A & );
Void fun0 (const A * );
Void fun1 () const; // fun1 () is a class member function.
Const A fun2 ();
2. const Initialization
Let's take a look at the initialization of the const variable.
1) Non-pointer const constant initialization:
A * d = new ();
Const A * c = d;
Or: const A * c = new ();
Reference:
A f;
Const A & e = f; // in this way, e can only access functions declared as const, but not General member functions;
[Thinking 1]: Is the following assignment method correct?
Const A * c = new ();
A * e = c;
[Think 2]: Is the following assignment method correct?
A * const c = new ();
A * B = c;
3. the const modifier used as the parameter and Return Value
In fact, the principle is the same for both parameters and returned values. The const variable is initialized when a parameter is passed in and when the function returns.
1. Modify the const of the parameter, such as void fun0 (const A * a); void fun1 (const A & );
When you call A function, use the corresponding variable to initialize the const constant. In the function body, perform the constant operation according to the Section modified by the const. For example, if the parameter is const a *, the content of the passed pointer cannot be changed to protect the content pointed to by the original pointer. If the parameter is const A & a, the passed referenced object cannot be changed, protects the attributes of the original object.
[Note]: The const parameter is usually used when the parameter is a pointer or reference;
2. Modify the const of the returned value, such as const A fun2 (); const A * fun3 ();
After the return value is declared, const is modified according to the "Modification Principle" to provide corresponding protection.
The return value can be modified with const to prevent the following operations:
Rational a, B;
Radional c;
(A * B) = c;
The const is usually used to modify the return value to the object itself (non-referenced and pointer). It is often used when the binary operator reloads a function and generates a new object.
[Conclusion] In general, when the return value of a function is an object and declared as const, it is mostly used for the overloading of operators. In general, it is not recommended to use const to modify the type of the function's return value to an object or to reference an object.
The reason is as follows:
If the returned value is that an object is const (const A test = A instance) or an object is referenced as const (const A & test = A instance), the returned value has the const attribute, the returned instance can only be A public (protected) data member and A const member function in category A, and the value assignment operation is not allowed, which is rarely used in general.
[Think 3]: Can I define a value assignment operator to overload a function?
Const A & operator = (const A & );
Use of const in class 4 member functions
Generally placed behind the function body, such as void fun () const;
If a member function does not modify the data member, it is best to declare it as const, because the const member function cannot modify the data member. If it is modified, the compiler reports an error, this greatly improves the robustness of the program.
5. const suggestions
1. Be bold in using const, which will bring you endless benefits, but the premise is that you must understand the original principles;
2. Avoid the most common assignment errors. For example, assign values to the const variable;
3. When using const in parameters, you should use references or pointers instead of common object instances, for the same reason as above;
4. const should be well used in three member functions (parameters, return values, and functions;
5. Do not set the function return value type to const easily;
6 except for the overload operator, do not set the return value type as a const reference to an object;
My level is limited, welcome to criticize and correct, you can contact kangjd@epri.ac.cn
[Questions and Answers]
1. This method is incorrect because the declared pointer aims to change the content it points to, and the declared pointer e points to a constant, so it is incorrect;
2. This method is correct because the content pointed to by the Declaration pointer is variable;
3. This is incorrect;
In const A: operator = (const A & a), the const In the parameter list is correctly used. When such A value is assigned consecutively, the problem occurs:
A a, B, c:
(A = B) = c;
Because the return value of a. operator = (B) is a const reference to a, c cannot be assigned to a const constant.
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.