C + + 's const

Source: Internet
Author: User

Many times we need to define a variable, and its value cannot be changed, and we need to use the Const qualifier.

First, the initialization of the const object

The const object must be initialized when it is created.

Because a const object cannot be changed after it is created, it should be initialized when it is created.

1 Const int 7 // correct 2 8; // Error: Cannot modify const object 3 Const int i2; // error: I2 uninitialized

You can assign a value to a variable with a const object without modifying the Const object:

1 Const int i3=7; 2 int I4=i3; // correct, the "Do not modify the const object under condition" means that I4 is not a const object

This is because although i3 is a const object, its value is actually an int, the copy i3 to I4 assignment does not change i3, and after the copy is complete, I4 and i3 have no relationship.

Second, const and reference

A reference to a const is often referred to simply as a constant reference

The referenced type should be the same as the type of the object it refers to, but there is an exception here in const, which is:

When you initialize a constant reference, you can use any value as the initial value, as long as the value can be converted to the referenced type.

1 inti5 =Panax Notoginseng;2 Const intI6 = +;3 Const int&R1 = i5;//correct: Bind const int& to a normal int object4 Const int&R2 = I6;//correct5 Const int&R3 =Panax Notoginseng;//correct6 Const int&R4 = i5 +2;//correct

What happened in such an exceptional situation?

In fact, for

int i5=Notoginseng; Const int &r1=i5;

In this case, the compiler does this:

1 int i5=notoginseng; 2 Const int temp=i5; 3 Const int &r1=temp;

The other situation is similar.

So, when a const reference is bound to a very good amount, it only guarantees that the extraordinary amount is not changed by this const reference, but there are other ways to change that very amount. Cases:

1 int 7 ; 2 Const int &r5 = i8; 3 8; // Error: R5 is a constant reference and cannot be changed 4 int &r6 = i8; 5 9; // correct: R6 is a common reference

So if you want to reference a const object, you have to use a constant reference, but a constant reference does not necessarily have to be a const object

Third , pointers and const

Combine pointers and const to differentiate between pointers to constants and constant pointers

1. Pointers to constants: refers to a pointer that cannot be changed by the pointer itself to the object it refers to (and does not require that the object be a const object), at which point the * is placed after the const to represent:

 1  const  int  I9 = 7  ;  2  int  J1 = 8  ;  3  const  int  *P1 = &i9; //  correct  4  const  int  *P2 = &j1; //  Right, you can bind a pointer to a constant to a very amount of  5  int  *p2 = &i9; //  error: cannot point to constant object with normal pointer  

Like a constant reference, a pointer to a constant does not qualify the object it is pointing to must be a constant, so a pointer to a constant only guarantees that the object it refers to is not changed by the pointer itself, but it may also be changed by other means:

1 int 7 ; 2 Const int *p3 = &J2; 3 8; // Error: P3 is a pointer to a constant 4 int *p4 = &J2; 5 9; // correct: P4 is a normal pointer

So, to store the address of a constant object, you have to use a pointer to a constant, however, a pointer to a constant does not necessarily have to hold the address of a constant object

2. Constant pointer: The pointer is also an object, so the pointer itself is allowed to be defined as a constant, at which point * is placed before the const to represent:

1 int 7 ; 2 INT *const P5 = &J3; 3 1111; // Error: P5 is a constant and cannot be changed 4 9; // correct: P5 is not pointing to a constant

3. Constant pointer to constant: the mixture of the above

 1  const  int  J4 = 7  ;  2  const  int  *const  P6 = &j4; //  correct: the const object can only be pointed to by a pointer to a constant to the  3  int  J5 = 7  ;  4  const  int  *const  P7 = &j5; //  correct: this is because pointers to constants do not necessarily point to const objects  

Iv. top-level const and underlying const

Because the pointer itself is another object, it can point to another object, so the pointer itself is not a constant and the pointer refers to the object is not a constant is two independent problems.

top-level const: indicates that the pointer itself is a constant

underlying const: indicates that the pointer refers to an object that is a constant

Therefore, pointers can be either top-level const or the underlying const

Note: The underlying const is used to declare references

1 intJ6 =7;2 Const int*P8 = &j6;//for the underlying const3 int Const*P9 = &j6;//is the top-level const4 Const int*ConstP0 = &j6;//The left is the underlying const, and the right is the top-level const5 Const int&R5 = J6;//underlying const

The top-level const and the underlying const differ significantly in the copy operation:

In a copy operation, the object being copied (the left of the equal sign) and the copied object (right side of the equal sign) have a top-level const qualification, but must have the same underlying const qualification, or the data type of two objects can be converted, and again, the very amount can be converted to a constant, and vice versa.

1 intJ7 =7;2 Const int*Q1 = &j7;//underlying const3 int*Constq2 = Q1;//Error: Q2 has a top-level const qualification but does not have the underlying const qualification and cannot be initialized with a Q1 with the underlying const qualification4 Const int*ConstQ3 = Q1;//correct: The Q3 has the underlying const and top-level const qualifications and can be initialized with a Q1 with the underlying const but not the top-level const qualification5 Const int*q4 = &j7;//correct: int * can be converted to const int*

C + + 's const

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.