Learning C Language Foundation must know that variables and data types are simple but the knowledge, but this foundation does have a few mire, a little careless of the pieces-the programming is blocked, the interview is brushed. One of them is the reference, the pointer and the const, and the relationship between the shear and the confusion of each other. Today we will have a reason.
1. What is a quote?1.1 Concepts of references
A reference is a different name for an object, that is, an alias. Then what is an object? Note that this is not the same thing as object-oriented objects. The object here is an area of memory, which has some type, and the variable is a named object. As you can see, referencing a simple relationship with an object is like a name and a person. The name 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 can be invented in a novel, but must have a display of the corresponding person.
In general, when initializing a variable, the initial value is copied to the newly created object, but when the reference is defined, the program binds the reference to its initial value, rather than copying the initial value directly to the reference, i.e., the reference points to the object, but the data is in one copy. Once the initialization is complete, the reference is bound to its initial 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 reference refval of the ival. The third line is wrong, the reason is the above point one: uninitialized.
It is important to note that values that cannot be unbound, must be initialized, and do not represent reference types, cannot be changed, and in fact the changes are more varied. once 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:
<span style= "FONT-SIZE:18PX;" >int i,&ri=i;i=5;ri=10;cout<<i<< ";" <<ri<<endl;</span>
This binds ri to I, where I is used to initialize the RI, but in fact it is not initialized in the first row I, which is okay. Just like the newlyweds give their children names, but the children don't yet, but it doesn't matter, the name belongs to the future born child.
The middle two lines are assignments, and if in this order, the last I and RI values are 10. Conversely, what if the middle two lines are reversed? The values for that I and RI are all 5. The reason is also point two.
The const itself is not complex, it is difficult to practice, once with pointers, functions and const, and so on together, often have a very nerve-racking problem. What is a const? the definition given in C + + Primer isthatconst is a type modifier that describes an object that never changes
1.2 definition and use of references
The referenced symbol is "&", and for the reference definition, remember two points: the initial value of the reference must be an object (point three), the definition type of the reference type is the same as the initial value object type (Point four).
For example: ①int refval4=10; is wrong because of the point three.
②double dval=3.14;
int &refVal5=dval; Was wrong because of the point four.
See a few examples:
③int Ival=1.01;int &rval2=ival; This code is correct, because the value of ival is the integer type 1, not 1.01, but this code has hidden trouble, easy to produce unexpected problems.
④int i=0, &r1=i;
Double d=2.1, &r2=d; see two assignments:
R2=R1, so write is right, for what, I feel, this is in fact, with a common number R1 to R2 assignment, and r2=0 no difference.
R1=d, this is also correct, in fact, this is the assignment statement r1=2.1, but the final result is forced to convert the type to 2.
Let's look at a more specific example:
int I, &ri=i, &r2=ri;
i=5;
here, &r2=r1, the compilation under GCC is correct, but does it mean that references can be defined for references? According to the C + + Primer, the reference itself is not an object, so reference references cannot be defined, but the result of the final output is three variable values of 5. I feel that the RI should point to I, which is actually a reference to the I definition.
I tested two complete codes for the above features:
#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;
The second one:
#include <iostream> using namespace Std;int
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. Pointers are very detailed and most people should know that here we focus on the differences between the references.
The pointer definition is also simple:
int ival=42;
int *p=&ival.
The more deadly is that there is a "&", the pointer indicates the address of an object, beginners are easy to mix with the reference. The above code can be written in the following form to make it 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 the cout<<ival.
One of the keys to confusing pointers and references is that the symbols "&" and "*" can have multiple meanings.
on the left side of the equals sign, the symbol appears as a type name, which is part of the declaration (Point 5.1). If the symbol is in an expression to the right of the equal sign, it is either the address (&) or the parsed (*). (Point 5.2)
int i=42;int &r=i;//Essentials 5.1int *p;//Essentials 5.1p=&i;//Point 5.2*p=i;//here is actually with the constant I to *p assignment, "C + + Primer" said that this is a solution reference, do not understand, please master pointing int &r2=*p;//here is the same as int &r2=42;, which assigns a reference R2 to the object that the address P points to.
Reference to pointer The reference itself is not an object, so a pointer to a reference cannot be defined, but the pointer is an object, so there is a reference to the pointer. For exampleint i=42;int *p;int *&R=P;//R is a reference to a pointer. This code is difficult to understand, point six: Complex expressions are 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 to the address of I, so assigning R is another p pointing to I. *r=0;//the R to get I, which is the object that P points to, change the value of I to 0. Be careful with the above two types of code. Personal feeling understanding the main line of large-scale software is to figure out how the data from start to finish, if you encounter this code sometimes found that the program suddenly broken, big headache.
3 Const Qualifier
A const is a type that describes an object that never changes, which means that once a const object is defined it cannot be changed, and it must be initialized when it is naturally defined. The definition and use of Const is not mentioned, it focuses on const with reference and const with pointers.
3.1const and References
Binding a reference to a const is called a reference to a constant. Given the characteristics of a const, a reference to a constant cannot be used to modify the object to which it is bound.
the const int Ci=1024;const int &r1=ci;//is correct, and the reference and its corresponding object are all constant int &r2=ci;//c errors, attempting to point a const reference to a constant reference. Notice the difference between these two lines of code
<pre name= "code" class= "CPP" >r1=42;//error, R1 is a reference to a constant
The const requirement is strict and cannot be initialized with a reference to a constant to a normal reference. For example:
<span style= "FONT-SIZE:18PX;" >int i42;const int &r1=i;const int &r2=42;const int &r3=r1*2;int &R4=R1*2;//R4 is a very important reference and cannot be initialized with a constant reference. </span>
In a normal reference, the type of the reference must be the same as the type of the object it refers to. However, at initialization time, any expression can be used as the initial value as long as the type can be converted to the past. What do you 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 understood by combining coercion type conversions.
Since RI refers to the number of an int, but Dval is a double-precision floating-point, there is a cast in fact:
const int Temp=dval;
const INT &ri=dval;
This is where the RI is bound to a temporary volume object. But although this way can compile the past, but there is no meaning, there are hidden dangers. C + + code is a bit more rigorous good.
3.1const with Pointer
The const pointer is much easier to understand, which is to define a pointer as an immutable constant. We know that a pointer can represent an object and the address of an object, and it is clear that the const pointer can indicate whether the address itself is a constant or a constant, and the former in C + + Primer is called the top-level const, which is called the underlying const.
First look at a code:
Const double pi=3.14;double *ptr=π//error, with constant address to the normal pointer initialization, similar to the White horse instead of all horses, generalize.
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, use only pointers to constants.
For example:
const double pi=3.14;
Const double *cptr=π*cptr=42;//error. You cannot assign a value to a constant address. Here is the use of *p=42 or what way, can not.
(not to be continued)
The love and hate between references, pointers, and Const in C + +