C++ constant details the initialization of of constant Pointers and constant references

  • 2020-05-24 05:51:40
  • OfStack

1, the constant

1.1 initialization of constants:
The value of const object 1 cannot be changed once it has been created, so the const object must be initialized. Here we have to pay attention to 1 point, like const int *p and const int & None of r is an const object. Since const int *p only means that the value of the object referred to by p cannot be changed by p, the value of p can be changed, so p does not need to be initialized. As for r, the reference itself is not an object, so r is not an const object. r 1 is definitely initialized because the reference must be initialized. For the above, you can also understand that the underlying const is not an const object, but also notice that something like const int *const p1, which is definitely initialized by 1, is an const object, because it has a top-level const.

1.1.1 after distinguishing const objects, let's focus on const objects.

const objects can be divided into roughly three categories:


 const int m
 int *const p
 const int *const q

The first two classes can be used to initialize non-const objects or const objects, because the first two classes are top-level const objects, and the copy at the time of initialization will not change their own values, that is, the following code is correct.


// The first 1 class 
int m1=m;// correct 
const int m2=m;// correct 
// The first 2 class 
int *p1=p;// correct 
int *const p2=p;// correct 
const int *p3=p;// correct 
const int *const p4=p;// correct   

And the third category needs our attention 1, because the third category also has a bottom const.
const int *const q


int *q1=q;// An error 
int *const q2=q;// An error 
const int *q3=q;// correct 
const int *const q4=q;// correct 

The underlying const has one limitation: when copying an object, the objects copied in and out must have the same underlying const.
If we can copy const int *const q and initialize it to q1 and q2, then we can change the value of the object q refers to by q1 and q1. So what is the meaning of const at the bottom of q?

Ok, so that's what these 3 classes can initialize, so let's look at what we can initialize for these 3 classes

Class 1: const int m


// The following 1 Some variables are not initialized, I focus on the type. 
int m1;
const int m2;
const int m=m1;// correct 
const int m=m2;// correct 

Class 2 int *const p


// The following 1 Some variables are not initialized, I focus on the type. 
int *p1;
int *const p2;
const int *p3;
const int *const p4;

int *const p=p1;// correct 
int *const p=p2;// correct 
int *const p=p3;// error 
int *const p=p4;// error 

This error is also because the underlying const has one limitation: when copying an object, both the object to be copied in and the object to be copied out must have the same underlying const

Class 3 const int *const q


// The following 1 Some variables are not initialized, I focus on the type. 
int *q1;
int *const q2;
const int *q3;
const int *const q4;

const int *const q=q1;// correct 
const int *const q=q2;// correct 
const int *const q=q3;// correct 
const int *const q=q4;// correct 

1.1.2 now let's turn our attention back to the initialization of non-const objects
There are two main categories:


// The following 1 Some variables are not initialized, I focus on the type. 
const int *p;
const int &r;

First consider which objects they can initialize
Class 1: const int *p


// The following 1 Some variables are not initialized, I focus on the type. 
int *p1=p;// An error 
int *const p2=p;// An error 
const int *p3=p;// correct 
const int *const p4=p;// correct 

This is again limited by the low level const

Class 2: const int & r


// The following 1 Some variables are not initialized, I focus on the type. 
int &r1=r;// error 
const int &r2=r;// correct 

Again, the reference is not an object, so it's not like int *const & r or const int *const & r

Ok, so let's think about what we can initialize for them
Class 1 const int *p


// The following 1 Some variables are not initialized, I focus on the type. 
int *p1;
int *const p2;
const int *p3;
const int *const p4;

const int *p=p1;// correct 
const int *p=p2;// correct 
const int *p=p3;// correct 
const int *p=p4;// correct 

Because the meaning of const int *p means that the value of the object it refers to cannot be changed through p, this object can not be a constant, but the object referred to by p is a constant for p, so it does not matter whether the object it refers to is const.

Class 2 const int & r


// The first 1 class 
int m1=m;// correct 
const int m2=m;// correct 
// The first 2 class 
int *p1=p;// correct 
int *const p2=p;// correct 
const int *p3=p;// correct 
const int *const p4=p;// correct   
0

Here again, const means that you cannot change the object it refers to through r, so it doesn't matter whether the object it refers to is const or not.
All of these errors are attempts to initialize the underlying const copy to an object that does not have the underlying const.
That's all for initialization of Pointers and references to constants.


Related articles: