C++Primer Fifth Edition: Exercise 2.27 2.28 2.29 2.30 2.31 2.32

Exercise 2.27
(a) Illegal, reference bound objects cannot be bound to literal constants
(b) legal, the underlying const pointer refers to a constant
(c) illegal, references cannot be bound to literal constants
(d) legal, pointers are both The top layer is also the bottom layer const
(e) is legal, the bottom const cannot change the object pointed to by the pointer
(f) is illegal, the reference is not initialized (bound object), the reference itself is not an object
(g) is legal

Exercise 2.28
(a) is illegal, the pointer is the top-level const to be initialized
(b) is illegal, the p2 pointer is the top-level const to be initialized
(c) is illegal, the constant ic is not initialized, and the r reference cannot change its value
(d) is illegal, The pointer is the top-level const to be initialized
(e) legal, the bottom-level const pointer

Exercise 2.29
(a) legal, constant assignment to non-constant
(b) illegal, object constant pointer cannot be assigned to non-low-level const pointer
(c) illegal, constant cannot be assigned to non-low-level const pointer
(d) legal
(e) Legal, the top-level const pointer (the value of the object can be changed
) is not legal to assign to the ordinary pointer (f), ic is a constant and cannot be changed

Exercise 2.30

#include<iostream>

int main()
{
    
    
	int i = 0;
	const int v2 = 0;
	int v1 = v2;
	int* p1 = &v1, & r1 = v1;
	const int* p2 = &v2, * const p3 = &i, & r2 = v2;
}

v2 top const
v1 bottom const
p1 r1 bottom const
p2 bottom const
p3 bottom top const
r2 bottom const

Exercise 2.31
r1=v2 is legal, the top-level const is copied to a normal reference, the top-level does not pass
p1=p2 is not legal, the bottom-level const pointer cannot be assigned to a non-bottom-level const pointer
p2=p1 is legal, the normal pointer can be assigned to the bottom-level const pointer
p1=p3 is not Legal, the underlying const pointer cannot be assigned to a non-low-level const pointer
p2=p3 is legal, the underlying const pointer can be assigned

Exercise 2.32

#include<iostream>

int main()
{
    
    
	int null = 0, * p = &null;
}

Illegal, modify as above

Guess you like

Origin blog.csdn.net/Xgggcalled/article/details/108916866