Const! Pointer!

Source: Internet
Author: User

Const implementation

Const is only valid for the compiler. Strictly speaking, const does not "cannot be modified", but informs the compiler that my variable should not be modified. If the compiler finds that the program attempts to modify this variable, an error is reported. In some cases, the const variable may be modified by something other than the compiler, such as hardware interruption.

1. pointer to the const object
You can understand the pointer to the const object as follows: the pointer to the const object is a pointer and cannot be used to modify the value of the object to which it points.

Declaration method: const int * p;

The value of a const object cannot be modified during initialization. Therefore, we cannot use a normal pointer to direct a value under a const object, which may cause an error.

Const int I = 1;

Int * p = & I;

If you can use a normal pointer to modify the value, it means that changing the value of the const will be meaningless.

Correct usage

Const int I = 1;

Const int * p = & I; (or int const * p = & I)

In this way, the value of the const object cannot be modified by using the pointer pointing to the const object.

Note the following two points:

The pointer to the const object itself is not the const type (this is also the main difference between it and the const pointer), so it can point to another const object

A pointer to a const object can be assigned a non-const object address. However, it is illegal to use this pointer to modify the object value.

 

2. const pointer

The const pointer can be understood as follows:

The const pointer is a pointer and itself is of the const type. Therefore, after initialization, you cannot change the pointer to a new object.

Declaration method:

Int * const p; // The const pointer to a non-const object

Const int * const p; // const pointer to the const object

From the above declaration method, we can see that the const pointer can point to the const object and non-const object, but the Declaration methods of the two are different.

You cannot use the const pointer to modify its address value. However, if the const Pointer Points to a non-const object, you can use it to modify the value of the object to which it points.

PS:

If the const is on the left side of *, 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 *, const is to modify the pointer itself, that is, the pointer itself is a constant.

Summary:

If the keyword adjacent to the pointer name is const, It is a const pointer. If the object type to which the pointer is declared has the const keyword, It is a pointer to the cosnt object.

For more information, see Objective c ++. If the const is on the left side of the asterisk, the 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, const is to modify the pointer itself, that is, the pointer itself is a constant

 

See: http://luoypeng.blog.163.com/blog/static/472754042010101434959127/

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.