A pointer to an object in C + + pointing to a constant pointer to a common object _c language

Source: Internet
Author: User
Tags constant

A constant pointer to an object

Declare the pointer variable to the object as a const and initialize it so that the pointer value remains at its initial value and cannot be changed.

Copy Code code as follows:

Time T1 (10,12,15), T2;
Time * Const ptr1=&t1;
ptr1=&t2;

The general form of a constant pointer to an object is defined as

Class name * Const pointer variable = object address;

Note that you should initialize the pointer variable when it is defined


The value of a constant pointer variable that points to an object cannot be changed, that is, always pointing to the same object, but can change the value of the data member (not the const type) in the object to which it points.

It is often used as a formal parameter of a function to not allow changing the value of a pointer variable to always point to the original object during function execution. If the value of the parameter is modified during the execution of the function, the compiler will have an error and give the error message, which is more reliable than the manual method to ensure that the parameter values are not modified.
Pointer to a constant object

If a variable has been declared as a constant, you can only point to it with a pointer variable that points to the constant, not with a normal (non-const) pointer variable.

Copy Code code as follows:

const int a;//Define constant variable a
const int *p;//defines pointers to constant variables
p=&a;

This is also true for objects, and if you declare an object to be a regular object, you can only point to it with a pointer to a normal object, not a normal (non-const) pointer variable.
Copy Code code as follows:

Const time t1;//defines a constant object
Const time *p;//defines a pointer to a constant object
P=T1;

Defines the format of a pointer variable that points to a constant object

Const class name * pointer name;

The following points need to be noted:

(1) A pointer to a constant object can point to a non-const object, but the object to which it is directed cannot be changed by a pointer.

A constant object can only be pointed at by a pointer to a constant object, as seen in the table:

(2) Note the difference between a pointer to a regular object and a constant pointer to an object
Copy Code code as follows:

Time * Const p;//A constant pointer to an object
Const TIME * p;//Pointer to a constant object

(3) A pointer to a regular object is most commonly used in a function's formal parameters to protect the object pointed to by the parameter pointer so that he is not modified during function execution.

(4) If you define a pointer variable that refers to a constant object, you cannot change the value of the object you are pointing to, but the value of the pointer variable itself can be changed.

Copy Code code as follows:

The const time *p=&t1;//defines the pointer variable p that points to a constant object and points to the object T1
p=&t2;//p to point T2, legal

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.