C ++ pointer to const object and const pointer

Source: Internet
Author: User

1. pointer to the const object
We can use a pointer to modify the value of the object it refers to. However, if the pointer points to a const object, it is not allowed to use a pointer to change the const value it refers. C ++ requires that the pointer to the const object be of the const type.

Const double * pd; // you can point to a const double type.

 

Here, const limits the objects referred to by pd, rather than pd. The pd here can point to another address again, but it cannot be used to change the value of the object to which it points.

Assigning the address of a const object to a pointer of a non-const object will cause a compilation error, for example:

Const int a = 8;

Int * p = & a; // error

Const int * cp = & a; // OK

You cannot use the void * pointer to save the address of the const object. You can use a const void * pointer to save the address of the const type.

Assign a non-const object address to a pointer to the const object:

Int c = 3;

Cp = & c;

Although c is not of the const type, modifying the c value using cp may cause compilation errors. Cp is defined as a pointer to a const object, so cp cannot change the value of the object it refers.

The pointer to a const object is not necessarily a const object. Therefore, the value of the object cannot be changed. After all, the object referred to by it can be a non-const object, you can use other methods to change its value.

You can think of a const object as a pointer to a const object.

In actual application, pointers to const objects are often used as function parameters, which ensures that the objects referred to by the real parameters are not changed in the function.

2. const pointer
The const pointer indicates that the pointer cannot be changed.

Int num = 4;

Int * const p = & num;

P can be understood as a const pointer pointing to the int type. This means that p cannot be directed to other objects, and any operation that tries to assign values to p leads to compilation errors. The const pointer must be initialized during compilation.

If the pointer is const, you cannot modify the value of the object it refers. If the const Pointer Points to a non-const object, you can use this pointer to modify the value of the object, for example

Int e = 9;

Int * const p = & e;

* P = 6;

In this way, the value of e is changed to 6.

There is also a const pointer to the const object, which is used as follows:

Const int d = 4;

Const int * const p = & d;

In this way, the value of p cannot be modified, and the value of p pointing to the object cannot be modified.

 

Why should we use pointers and typdef? Using typedef is like a slap in the face and confusing yourself. Avoid using typedef to define pointers .....

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.