About const constants, const pointers, and const references

Source: Internet
Author: User
Tags numeric value

This blog from beginning to end to talk about the const some of the problems, I am programming small white, write a bad place also please point out.

1.const Constants

Unlike variables, the amount of a const modifier is a constant, the value of a constant is immutable, it must be initialized when it is defined, and it is wrong to use an uninitialized constant. The problem that was often unclear was the assignment of constants and variables, such as the following code:

int a=3;
const int b=a;
A is a variable, and B is a constant, can you assign a value to a constant with a variable? The answer is yes, the variable here only plays the role of assignment, and constant b is only initialized with the value of a, which is equivalent to a constant b initialized with a specific numeric value. The following code is also available:
int A;
const int b=6;
A=b;
It is also true that only the value of B is used to assign a value to a.

In addition: const int A has the same meaning as the int const A.

2.const pointer

Let's start with a pointer: A pointer is an object (different from a reference) and a composite type, and the pointer is decorated with the * operator. The pointer has its own space address, and the pointer holds the address of the object, and different types of pointers can point to a particular type of object, that is, the type of the pointer is the same type as the object being pointed to. You can change the value of the object you point to by using a pointer.

int A;
int *p=&a;
*p=5;
cout<<a<<endl;

The previous example code output is 5, which means that the value of object A is changed by a pointer.

If

Double f=3.124
int *p=&f;
A compilation error occurs because the object does not match the pointer type.

Constant pointers and pointer constants:

Constant pointers:

const int *p;
int i=34;
int j=90;
p=&i;
P is a constant pointer. It should be noted here that, although the literal meaning of the object P points to is a constant, that I should be a constant, rather than a variable of the above code, then p points to the variable i why? In fact, p "self-righteous" that they are pointing to the object must be constant, but it is not. A constant pointer can either point to a constant or a variable, and the true meaning of a constant pointer is that it cannot change the value of the object by P, and does not mean that the object's value cannot be changed by other means. Like what:
const int *p;
int i=34;
int j=90;
p=&i;
int *r=&i;
*r=8;
The pointer p and R point to variable a at the same time, and it is correct to change the value of object I through pointer R. If you add another sentence: *p=9, it is wrong because the constant pointer does not allow you to modify the value of the object by itself.
const int *p;
int i=34;
p=&i;
int *r=&i;
*r=8;
*p=9;

The address that the constant pointer points to can be changed, and the P pointer first points to I and finally to J:

const int *p;
int i=34;
int j=98;
p=&i;
p=&j;
Pointer constants:
int i=89;
int * Const p=&i;
The address stored in the pointer p cannot be changed, that is, p can only initialize, cannot change the object to which it is directed, and the value of the object can be changed by P. The following code is wrong:
int i=89;
int * Const p=&i;
int j=56;
p=&j;

3.const Reference

References: References are aliases, decorated with "&", where & is not an address character, but rather a composite type--reference. The reference must be initialized, and the object's value can be changed by reference.

int A;
int &b=a; 
cout<< "&a:" <<&a<<endl; 
cout<< "&b:" <<&b<<endl;
b=4;
cout<< "A:" <<a<<endl;
cout<< "B:" <<b<<endl;

B is the same as the output address of a, at this point B is an alias for a, they occupy the same memory space, modify the value of B, it is equivalent to modify the value of a.

Just beginning to contact the reference, C + + primer This book says, a reference is not an object, but an alias to an object, so a reference cannot define a reference reference as defined as "pointer", for example: We all have a name, some people have a nickname, they refer to a person, But the ID card can only fill in your name, and can not fill in the nickname, the reference is this truth.

Once a reference is bound to an object, the object cannot be changed halfway:

int A;
int &b=a; 
cout<< "&a:" <<&a<<endl; 
cout<< "&b:" <<&b<<endl;
int i=5;
b=i;
cout<< "&b:" <<&b<<endl; 
cout<< "&i:" <<&i<<endl;
B's address did not change, only the value changed.

A reference can only be bound to an object and cannot be bound to the result or literal value of an expression, as follows:

int &ra=1024;

Const reference:

A const reference is a reference to a const, which in fact is not appropriate, as with a const pointer, a const reference can be bound to a constant or to a variable, except that the value of the bound object cannot be changed by the const reference, as the const pointer above.

<span style= "FONT-SIZE:12PX;" >int a=10;
const INT &ra=a;
a=5;//correct
ra=4;//error, cannot change A's value by reference </span>

A non-const reference cannot be bound to a const object because the value of constant a cannot be changed, but it is wrong to change the value of constant a by a non-const reference, as follows:

<span style= "FONT-SIZE:12PX;" >const int a=10;
int &ra=a;//error, non-const reference cannot be bound to a const object </span>

The referenced type must be the same as the referenced object type, with the exception that any expression can be used as the initial value when the constant reference is initialized, as long as the result of the expression can be converted to the referenced type.

int i=42;
const INT &r1=i;//correct
const int &r2=42;//Correct r1 is const reference
const int &r3=r1*2;//Correct r1 is const reference
int &r4=32;        Error, R4 is not a const reference
Why is this happening? Let's find out. What happens when the const reference is bound to an object type that does not match the type:

Double d=3.1415;
const INT &rd=d;

This occurs when the const int &rd=d is executed: the const int TEMP=D;CONST int &rd=temp; A temporary variable is an unnamed object that is created temporarily when the compiler requires a space staging expression to evaluate the result. If Rd is not a constant reference, when you assign a value to Rd, you change the value of the object that Rd is bound to. But at this point the bound object is a temporary amount, it does not have a name, instead of binding d, since Rd is bound D, it is definitely want to change the value of D through Rd, otherwise why bind D to Rd. However, a non-const-Modified RD cannot change the value of D, and the binding is meaningless, so the compiler is not allowed to do so.
















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.