ZT const Usage Details

Source: Internet
Author: User

The usage of the const keyword in C ++ is very flexible, and the use of const will greatly improve the robustness of the program. I will summarize some of my experiences as follows and hope to help you:

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]
//// In fact, I think this only requires that the const priority is lower.

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 b;
const A a = b;

2) pointer (reference) 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.

Const rational operator * (const rational & LHS, const rational & RHs)
{
Return rational (LHS. numerator () * RHS. numerator (),
LHS. denominator () * RHS. denominator ());
}
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.

[Thinking 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;

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.