Have learned the basis of C language must know the variables and data types is simple but the knowledge, but there are a few of the foundation of the mire, a little careless on the shattered-programming blocked, the interview was brushed. One is the reference, the pointer and the const, and the relationship between the shear and the chaos. Let's have a reason for today. 1. What kind of ghost is the reference? 1.1 The concept of references
A reference is another name for an object, that is, an alias. Then what is the object? Note that the objects and objects in this object are not the same thing. The object here is an area of memory, it has some type, and the variable is a named object. It can be argued that a simple relationship between a reference and an object is like a name and a person. Names can be changed a few more, but must be with a real person, whether it is a male or female, is dead or alive, or even in a novel coined, but must have a display of the corresponding person.
Typically, when you initialize a variable, the initial value is copied to the newly created object, but when you define the reference, the program binds the reference to its initial value, rather than copying the initial value directly to the reference, that is, the reference points to the object, but the data is one. Once the initialization is complete, the reference is bound to its primary value object because the reference cannot rebind another object, so the reference must be initialized (point one).
Look at the following program:
int ival=1024;
int &refVal=ival;
int &refval2;
The second line defines the Ival reference refval. The third line is not right, the reason is the above point one: uninitialized.
It is important to note that the values that cannot be unbound and must be initialized that do not represent reference types cannot be changed, and in fact their changes are more varied. After a reference is defined, all operations on it are performed on the object to which it is bound (point two). For example, the following program:
int i,&ri=i;
i=5;
ri=10;
Here the RI and I are bound, here with I to initialize RI, although in the first row I did not initialize, it does not matter, then give me a value. Just as newlyweds give their children names, even though the children are not, it doesn't matter, the name belongs to a future born child.
The middle two lines are assigned, and in this order the values for the last I and RI are 10. Conversely, if the middle two lines are reversed. The value of both I and RI is 5. The reason is also point two. 1.2 definition and use of references
The referenced symbol is "&", and for the reference definition, remember the two principles: the reference's initial value must be an object (point three), and the reference type has the same definition type as the original Value object type (Point four).
For example: ①int &refVal4=10; is wrong because of the point three.
②double dval=3.14;
int &refVal5=dval; Is wrong, because the main point is four.
③int Ival=1.01;int &rval2=ival; This code is correct because the value of ival is an integer type 1, not 1.01, but the code is hidden and prone to unexpected problems.
Let's look at a more specific example:
int I, &ri=i, &r2=ri;
i=5;
Here, &R2=R1, compiling under GCC is correct, but does it mean that reference can be defined for reference? According to the C + + Primer, the reference itself is not an object, so the reference reference cannot be defined, but the result of the final output is three variable values of 5. I feel that RI should point to I here, and it's actually a reference for I.
I tested the two complete code for the above function:
#include <iostream>
using namespace std;
int Main ()
{
int i=1,&r1=i;
Double d=2.1,&r2=d;
r2=3.1415;
cout<< "R2:" <<r2<< ";d:" <<d<<endl; r2=r1; cout<< "R1: <<r1<<"; R2: "<<r2<<endl;
I=R2;
cout<< "I: <<i<<"; R2: "<<r2<<endl;
R1=d;
cout<< "R1:" <<r1<< ";d:" <<d<<endl;
return 0;
The second one:
#include <iostream>
using namespace std;
int Main ()
{
int i,&ri=i,&r2=ri;
ri=10;
i=5;
cout<<i<< "" <<ri<< ";" <<r2<<endl;
return 0;
2. What the hell is a pointer?
The pointer is not a joke, a book can not be explained (there is really Daniel's book, called "C and the Pointer"). You can simply assume that a pointer is the address of an object. The details of the pointer are very much, and most people should know, here we focus on analyzing the differences with references.
The pointer definition is also simple:
int ival=42;
int *p=&ival.
The most fatal thing is that there is a "&" in the pointer to the address of an object, beginners easy to mix with the reference. The above code can be written in the following form and is easier to understand:
int ival=42;
int *p;
p=&ival;
objects can be accessed through pointers, such as
cout<<*p; can output 42. The effect is the same as Cout<<ival.
One of the keys to confusing pointers and references is that the symbol "&" and "*" can have many meanings.
On the left side of the equal sign, the symbol appears as a type name and is part of the declaration (Point 5.1). If the sign is to the right of the equal sign, the symbol is in an expression, so the address (&) or parsed (*) is taken. (Point 5.2)
int i=42;
int &r=i;//Essentials 5.1
int *p;//points 5.1
p=&i;//points 5.2
*p=i;//here is actually using the constant I to the *p assignment, "C + + Primer" said that this is a reference, do not understand, Please master pointing
int &r2=*p;//Here is the same as int &r2=42;, assigning values to references R2 from the object pointed to by address p. </span>
The reference reference to the pointer is not itself an object, so a pointer to the reference cannot be defined, but the pointer is an object, so there is a reference to the pointer. such as int i=42; int *p; int *&R=P;//R is a reference to a pointer. This code is difficult to understand, point six: Complex expressions read from right to left. The symbol closest to the variable name has a direct effect on the type of the variable, here * (&r), so R is a reference, and then (&R) is a pointer. r=&i;//, the above point 5.2,r is a reference, pointing to the address of I, so assigning R is another p pointing to I. *r=0;//dereference R to get I, that is, the object P points to, change the value of I to 0. Be careful of the above two codes. Personal sense of understanding the main line of large software is to understand how the data go from start to finish, if encountered this kind of code will sometimes find the program suddenly broken, big headache.
3 Const Qualifier
Const is a type that describes immutable objects, which means that once a const object is defined, it must be initialized when it cannot be changed. The definition of const and the use of what is not mentioned, emphasis on the introduction of const and references and const and pointers.
When you perform a copy of an object, the objects that are copied and copied must have the same underlying (object that the pointer points to) const qualify, or the data type of two objects must be convertible. There is only one-way conversion: The extraordinary amount can be converted to a constant, otherwise not. 3.1const and Reference
Binding a reference to a const is called a reference to a constant. Given the const feature, a reference to a constant cannot be used to modify the object to which it is bound.
const int ci=1024;
The const int &r1=ci;//correctly, references and their corresponding objects are constant
int &r2=ci;//c errors, trying to make a very reference to a constant reference. Notice the difference between the two lines of code
r1=42;//error, R1 is a reference to a constant
The const requirement is more stringent and cannot be initialized with a reference to a constant to a normal reference. For example:
int i42;
const INT &r1=i;
const INT &r2=42;
const INT &r3=r1*2;
int &R4=R1*2;//R4 is a very literal reference and cannot be initialized with a constant reference. </span></span>
In a normal reference, the type of the reference must be the same as the type of the object it refers to. However, any expression can be used as an initial value as long as the type can be converted to the previous initialization. Well, what does it mean? Or look at the code:
Double dval1=3.14;
const int &r1=dval;//correct
int r2=4;
Const Double dval2=r2;//Error
In fact, this can be combined with coercion type conversion to understand.
Because RI refers to an int, but Dval is a double-precision floating-point number, this is actually a cast:
const int Temp=dval;
const INT &ri=dval;
This is where RI binds a temporary measure object. Although this way can compile the past, but there is no meaning, there are hidden dangers. C + + code is a bit more rigorous.
3.2 Const and pointers
The const pointer is much easier to understand than to define a pointer as an immutable constant. We know that a pointer can represent an object and an object's address, and it is clear that the const pointer can indicate whether the address itself is a constant or a constant, and the former is called the top const in C + + Primer, which is called the underlying const.
First look at a code:
const double pi=3.14;
Double *ptr=π//error, with the address of the constant to the normal pointer initialization, similar to the White horse instead of all the horse, sweeping. </span>
With respect to the const pointer, a pointer to a constant cannot be used to change the value of the object it refers to. To hold the address of a constant object, you can only use a pointer to a constant.
For example:
Const Double Pi=3.14;</span>
Const DOUBLE *cptr=π
*cptr=42;//error. You cannot assign a value to a constant address. It doesn't matter whether you use *p=42 or any other way.
Generally, the type of the pointer is the same as the type of the object it refers to. But a pointer to a constant can point to a very important object (otherwise not), thereby indirectly changing the value of the constant pointer. For example:
const double pi=3.14;
Onst Double *cptr=π
Double dval=3.333;
cptr=&dval;
Here Cptr point is Dval address, *cptr is 3.333. However, you cannot change the value of Dval by Cptr, and pointers to constants simply require that the object's value not be changed by the current pointer, but can be changed indirectly by other means.
What if you put * in front of the const? That's the const pointer, which means that the pointer is a constant, and its deep meaning is that the address itself is a constant and the value it points to may be changed.
For example:
int errnumb=0;
int *const Curerr=&errnumb;//curerr will always point to the errnumb address
int errnumb2=2;
curerr=&errnumb2;//error, no more values can be bound.
How to distinguish between the above two kinds of statements. Read from right to left according to the previous point six. The closest symbol here to Currerr is the const, meaning that the curerr itself is a constant object, that is, the address does not change, and then it is much easier to look at it.
Let's look at a more complex code:
Const DOUBLE *const pip=π
First the PIP is a constant pointer, and secondly, the object it points to is also a constant, and the type is a double-precision floating-point type.
4. Summary
The relationship between references, pointers, and const is a key topic in the basic syntax of C + +. The idea of solving the problem is to remember the characteristics of each point first, which is the summary of the above several points, and then proceed from right to left in order to gradually split. If you have an equal sign, remember that the meaning of the symbol around the equal sign will solve most of the problems.
Some of the above points are slightly improved:
Important one: References and Const define objects must be initialized, and pointers do not have to.
Point two: A reference to a definition in which all operations are performed on the object to which it is bound. The pointer is actually the address of an object, you can access the object through the pointer, or you can put the pointer (address) through an object.
Important three: The initial value of the reference must be an object, and the reference type has the same definition type as the initial value object type.
Point four: On the left side of the equal sign, the symbol appears as a type name and is part of the declaration. If the sign is to the right of the equal sign, the symbol is in an expression, so the address (&) or parsed (*) is taken.
Key five: Complex expressions read from right to left. The symbol closest to the variable name has a direct effect on the type of the variable.
Most of the above material comes from 2.3 and 2.4 of the second chapter of C + + Primer. However, I still feel that I have not completely explained the problem, even some places have loopholes. Although the blog has written for most of the day, the feeling is still quite a failure. After looking for a few interviews to meet the example of finishing and then slowly grinding the "love and Hate Feeling sorrow."