Const pointer summary, const pointer

Source: Internet
Author: User

Const pointer summary, const pointer

Const summary:
If the keyword const appears on the left side of the asterisk, it indicates that the theme is a constant. That is, the value of the variable cannot be modified through the pointer.
If the keyword const appears on the right of the asterisk, it indicates that the pointer itself is a constant. That is, the pointer cannot be changed.

If the theme is a constant, there are two forms:

Int a = 3, B = 4;

Const int * p = &;

Int const * p = &;

These two forms are equivalent.

For details, see const pointer.
Const pointer

I disagree with the answer from the upstairs. Char * const str defines a constant pointer to a string. The value of this pointer cannot be changed (that is, str cannot be assigned again ), but we can change the value of the variable pointed to by the pointer, that is, * str. the first error is not a pointer issue. Instead, the compiler places the string "abcd" in char * p = "abcd" in the constant area, and then sends the address to p, it cannot be modified.

Summary of the usage and functions of const in computer C ++

Const usage in C ++

1. const constants, such as const int max = 100;
Advantage: const constants have data types, while macro constants do not. The compiler can perform a type security check on the former, while the latter only performs character replacement without the type security check. In addition, unexpected errors may occur during character replacement.

2. Data Member of the const modifier class. Const data members are constants only within the lifetime of an object, but they are variable for the entire class. Because the class can create multiple objects, the values of the const data members of different objects can be different. Therefore, the const data member cannot be initialized in the class declaration, because the compiler does not know the value of the const data member when the class object is not created. Const data member initialization can only be performed in the class constructor initialization table.

3. For details about how to modify the pointer in const, see the following formula:
Const int * a = & [1]
Int const * a = & [2]
Int * const a = & [3]
Const int * const a = & [4]
You 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 const is on the right side of the asterisk, const modifies the pointer itself, that is, the pointer itself is a constant. Therefore, in the case of [1] and [2], the pointer points to a constant. In this case, you cannot change the content, for example, * a = 3; [3] 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 wrong; [4] indicates that the pointer itself and the content pointing to are constants.

4. const Initialization
Let's take a look at the initialization of the const variable.
1) Non-pointer const constant initialization: A B;
Const A = B;
2) const constant initialization:
A * d = new ();
Const A * c = d;
Or: const A * c = new ();
3) const constant initialization:
A f;
Const A & e = f; // in this way, e can only access functions declared as const, but not General member functions;

5. const is applied in function declaration. In function declaration, const can modify the return value of a function or a parameter. For member functions, it can also be modified as a whole function. 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, and can only modify the input parameter. If the input parameter uses the "value transfer" method, because the function will automatically generate a temporary variable for copying this parameter, this parameter does not need to be protected, so it does not need to be modified by the const.
[Conclusion] for input parameters of non-Internal data types, the "value transfer" method should be changed to "const reference transfer" to mention ...... remaining full text>

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.