C + + pointers and const

Source: Internet
Author: User

As with a reference, you can make the pointer point to a constant or to a very great amount. A pointer to a constant cannot be used to change the value of the object it refers to. The address of the constant object you want to store. You can only use pointers to constants:


Const	Double	pi	=	3.1.4;	Pi is a constant whose value cannot be changed	
	double	*ptr	=	&pi	//error: PTR is a normal pointer
const	DOUBLE	*cptr	=	&pi	//correct: Cptr can point to a double constant		
		*cptr	=	/	/error: Cannot *cptr assignment because it is a const-decorated pointer constant , cannot be modified




Do not try to change the value of the cptr to change the PI value. Because you think about it, Pi is a const-modified double type constant, and it can't be changed. If you can change the value of Cptr, then the PI value will be changed. The result will contradict the const modifier.


A pointer or reference to a constant that is merely a pointer or reference "self-righteous, they feel they are pointing to a constant, so consciously do not change the value of the object being referred to."


Const pointer

The pointer is an object and the reference is not, so, like other object types, allows the pointer itself to be set as a constant. Constant pointer, which must be initialized, and once initialized, its value cannot be changed. Put * before the const keyword so that the pointer is a constant,

const	int	errnumb		=	0;
	int	*const	curerr	=	&errnumb	//curerr will always point to errnumb
const	int	Pi		=	3;
	int	*const	pip	=	&pi			//Error: The const level is different, the pi is the top layer CONST,PIP is the underlying const, Pip cannot point to Pi because of the underlying const limit. The top layer cosnt
const	int	*const	pip	=	&pi		//Correct is explained later: PIP is a constant pointer to a constant object





The most effective way to find out what the declaration means is to read from right to left. The most recent symbol from Curerr is const, meaning that the PIP itself is a constant object, and the type of the object is determined by the remainder of the declaration. The next symbol in the declaration is *, meaning that curerr is a constant pointer. Finally, the base data type portion of the declaration statement determines that the constant pointer is pointing to an int object. Similarly, the PIP is a constant pointer that points to an integer constant.


Top-Level const


As mentioned earlier, the pointer itself is an object that can point to another object. Therefore, the pointer itself is not a constant and the pointer refers to a constant that is two independent of each other. Using a noun-top const to indicate that the pointer itself is a constant, and using the noun-bottom const to indicate that the object that the pointer refers to is a constant.


More generally, the top-level const can indicate that any object is a constant, which applies to any data type including pointers. The underlying const is related to the basic type part of a composite type such as pointers and references. More specifically, the pointer type can be either a top-level const or a bottom const, which is distinguished from other types:

	int			I	=	0;
	int			I2	=	ten;

	int		*const	p2	=	&i2//Cannot change the value pointed to by P2, which is a top-level
	const int		*const	pl	=	&i;	Can not change the value that PL points to, this is a top-level const
				PL	=	p2;	Error: Cannot change PL point value
				*pl	=	ten;	Correct: Can change the specific value of PL

const	int			ci	=	;	You cannot change the value of CI, which is a top-level const
const	int			*p2	=	&ci;//allows you to change the value that P2 points to, which is a lower
-level const const	int		*const	P3	=	P2;	On the right of the const top of the const, on the left is the underlying const
Const	int			&r	=	ci;	The const used to declare references is the underlying const




When an object copy operation is performed. Whether a constant is a top-level const or a lower-level const is distinct.

The limitations of the underlying const cannot be ignored. When the copy operation of an object is performed, the copied and cuffed objects must have the same underlying const qualification, or the data type of the two objects must be convertible. In general, the extraordinary amount can be converted to a constant.


<pre name= "code" class= "CPP" >	int	*p	=	P3;		Error: P3 contains the definition of the underlying const, while P has no
		p2	=	p3;		Correct: Both P2 and P3 are the underlying const
		P2	=	&i;		Correct: int* can be converted to const int*
	int	&r	=	ci;		Error: Normal int& cannot be bound to a
const	int	&r	=	i	on an int constant; Correct: const int & can be bound to an ordinary int




P3 is the top-level const, which is also the underlying const, and the copy film does not care that he is a top-level const, but it must be clear that the object it points to is a constant. Therefore, you cannot initialize P with P3, because p points to an ordinary (very important) integer. On the other hand, the value of the P3 can be assigned to P2 because the two pointers are the underlying const, although P3 is also a constant pointer (the top const) and will have no effect on this assignment alone.




Related Article

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.