So that you completely understand pointer, reference and const

Today relive a little C ++ Primer, have a clearer understanding of the three concepts above, self believe have a more comprehensive understanding, so I quickly recorded, also please criticism.

1. references

Reference, since the object is simply an alias, alias can be identical to the operation target, the type of reference defined by the specifier & written in the form of d, where d is the declaration of a variable name, i.e., an alias reference variables:

int i =1; int &r = i; //r指向i(r是i的别名,可以通过操作r来改变i的值)、 r=2; cout<<r<<" "<<i<<" "<<endl; //r和i的值都为2 int j=r; //j被初始化为i的值,即为2

Popular speaking, is a reference assignment, in fact, the value assigned to the object reference bound. Gets a reference value, in fact, is to get the value of the bound object references. But the reference itself is not an object, it can not be defined cited references.

In addition to references must be initialized, just said, in reference to the definition of the time, the program is now referenced and its initial value bound together, rather than copying the initial value to the reference. Once the initialization is complete, the reference value and its initial target has been bound together, because they can not make reference to rebind to another object, so references must be initialized.

int &r; //错误,引用必须被初始化 int &r=10; //错误,引用类型的初始值必须是一个对象 int i=10,&r=i; //正确,r是一个引用,与i绑定在了一起,i是一个int int j=0; int& r=j,p=j; //正确,r是一个引用,与j绑定,p是一个int

Stress here that there may be a few people ignore the problem, why the above code is not the last paragraph should r and p are references to it?
This idea is wrong, I deliberately quoted above, the symbol & wrote back int, without the written next variable, writing the results of the two are the same, r is a reference, p is int;
here we have to figure out a life sentence by a list of basic data types and character followed by a statement of the composition. Corresponds to the above piece of code is the basic data type is int, & declarator is the first variable declaration after the break only for its effective, so no matter how many characters we added in a statement later int, valid only for the first variable , independent variable and the back, for example:

int *&p,q; //p是一个引用,绑定一个指针变量,q是一个int int* p,i,&r=i; //p是指针,i是int,r是引用

There is also a more important point of reference is the type of reference with which the object to bind tight match. (There are two exceptions to the case, and in part will introduce a const)

int i=0,&r1=i; double d=3.14,&r2=d; double &r3=i; //错误,引用类型为double,对象类型为int r1=r2; //double型向int型转换,会丢失精度 cout<<i<<" "<<r1<<" "<<endl;//i和r1的值都为3

2. Pointer

Similar to a reference pointer, but also achieve indirect access to other objects. However, with reference to contrast, there are many different points, one pointer itself is an object, allowing pointer assignment and copy, but also has point to different objects (references Once bound can not be changed); secondly, when pointer no longer defined initial value.

Pointer is the address of an object, in order to obtain the address, the address code & need to take, and in general (with two exceptions, leave aside again), the type of the pointer points to which it must strict matching objects:

int a=1; int *p=&a; //p存放变量a的地址,或者说p是指向变量a的指针 double b=3.14; double *q; q=&b; //q是指向p的指针 int *r=&b //错误,指针类型与对象类型不匹配

Pointer values ​​(i.e., address) has four states:

  1. Point to an object
  2. Points to the next target position adjacent to the space occupied by (int * p = & i, p ++)
  3. Null pointer means the pointer does not point to any object
  4. An invalid pointer, i.e. a value other than the above

The consequences of an invalid pointer access will lead to error, but does not compile time error, run time before being given access to an invalid pointer is unpredictable, so the program must be given a clear pointer is valid.

Although the above in the form of the second and third pointer is valid, it is clear these pointers do not point to any object, it attempts to access an object pointer such behavior is not allowed, if this is done, the consequences can not be expected.

If the pointer points to an object, you can reference symbol (* operator) to access the object with the solution, and see an example below in conjunction with pointer references:

int i = 42; int &r = i; //r为i的引用 int *p; p = &i; //p是指向i的指针 cout<<*p<<endl; //输出42 *p = 32; //相当于给i赋值 cout<<i<<endl; //输出32 int &r2 = *p; //相当于int &r2 = i; cout<<r2<<endl; //输出32 r2=22; //改变i的值 cout<<i<<" "<<r2<<" "<<*p<<endl; //三者相等,都为22 *p=12; cout<<i<<" "<<r2<<" "<<*p<<endl; //三者相等,都为12 i=2; cout<<i<<" "<<r2<<" "<<*p<<endl; //三者相等,都为2 int j = 52; *p=j; //相当于将j的值赋给i,i=j; cout<<i<<" "<<r2<<" "<<*p<<endl; //三者相等,都为52 p=&j; //将p指向j, j=62; cout<<i<<" "<<r2<<" "<<*p<<endl; //i=r2=52,*p=62 r=j; 相当于i=j, cout<<i<<" "<<r2<<" "<<*p<<endl; //都为62

There may be some students to understand the meaning of & and * very vague, why would represent a way, one would then be another meaning. Indeed, such as & and * symbols, both as expressions of the operator, but also appear as part of the declaration, the symbol context determines the meaning of the symbol:

int i=2; int &r=i; //&紧随类型名出现,因此是声明的一部分,r是一个引用 int *p; //*紧随类型吗出现,因此是声明的一部分,p是一个指针 p=&i; //&出现在表达式中,是一个取地址符 *p=i; //*出现在表达式中,是一个解引用符 int &r2=*p;//&是声明的一部分,是引用,*是一个解引用符

Null pointer does not point to any object, a method defined null pointer:

int *p = nullptr;//推荐方法  int *p2=0; int *p3=NULL;//NULL的值就是0,等价int *p3=0

The best way to get a null pointer is the first, it is the new C ++ 11 standard method newly introduced, nullptr a is a value of a special type, it may be converted into any other type of pointer. A pre-processing variable is NULL, the preprocessor automatically replace it with zero. Under the new standard, it is best to use nullptr, at the same time try to avoid using NULL.

Also not directly assigned directly to the pointer variable int, int variable to 0 even nor

int z=0; int *p=z;//错误,不能把int变量直接赋给指针

As already mentioned, pointers, and references can provide indirect access to other objects, and then the two great differences in implementation details, reference is not an object in itself, once a binding target can not be changed. The pointer is different, as long as it is stored pointer assignment of a new address, it points to a new target:

int i=2; int *pi=0; //pi是空指针 int *pi2=&i; //pi2指向i int *pi3; //pi3的值无法确定 pi3=pi2; //pi3和pi2都指向同一个对象i pi2=0; //现在pi2不指向任何对象,但pi3还是指向i

A pointer to a pointer

Generally declarator number modifier is not limited, for example, can be a useful ** represent pointers to pointers, *** represents a pointer to a pointer to a pointer, and so on:

int i=1024; int *pi=&i; //pi指向int型的数 int **ppi=&pi; //ppi指向一个int型的指针

Output i, * and ** ppi pi values ​​are the same, all for 1024.

A pointer reference

As described above, reference is not an object, so there would not be a pointer to the reference, but the pointer is an object, there is a reference pointer:

int i=42; int *p; //p是一个未被初始化的指针 int *&r=p; //r是一个指向指针p的引用 r=&i; //相当于p=&i; cout<<i<<" "<<*p<<" "<<*r<<endl;//三者的值相等

For in the end what r the above type is to teach you a simple method, the nearest symbolic variable names (here &) determines the type of a variable, so here it is certainly a reference r, part specifier to determine r What type of reference is here *, it means that reference is a pointer, finally, declared data type is int, then, that is, r is a reference to an int pointer. After such a complex definitions are understood.

3.const qualifier

const is used to define constants, once the value of an object is defined as a constant, it can not change the object.

const int buff = 512; buff=1024//错误,试图向const对象赋值 const int i=get_size(); //正确:运行时初始化 const int j = 42; //正确:编译时初始化 const int k; //错误:k是一个未经初始化的变量 int h=12; const int m=h; //正确:h的值拷贝给了m int n=m; //正确:m的值拷贝了n

You can bind a reference to a const object, known as a reference to the constants. The difference is that ordinary reference, a reference to the constant can not be used to modify the object it is bound, and the other allows a constant reference to bind a const object, sub-par value, or even an expression:

const int ci=1024; const int &r1=ci; //正确:引用及其对应的对象都是常量 r1=42; //错误:r1是对常量的引用,不能修改 int &r2 = ci; //错误,非常量引用不能指向常量对象 int i=42; const int &r1=i; //正确:允许常量引用绑定到普通int对象上 const int &r2=42; //正确:r2为常量引用 const int &r3=r1*2; //正确:r3为常量引用 i=2; cout<<r1<<" "<<i<<" "<<r3<<endl; //r1=i=2,r3=84; double d = 3.14; const int &r4 = d; //正确:r4为一个绑定到int对象的常量引用 cout<<d<<" "<<r4<<endl; //d=3.14,r4=3 d=5.15; cout<<d<<" "<<r4<<endl; //d=5.15,r4=3

It must be wondering, why the type to be consistent on both sides at the time of the reference, and to point to an object, but here and there in such a case, even if a specific point in different types and values ​​can be. To understand why the person is, we should find out when a constant reference in the end happened when bound to another type what:

At the end of that piece of code, the compiler actually r4 binding in order to ensure an integer object compiler introduces a temporary variable, the code will become the following form:

double d=3.14; const int temp=d; const int &r4=temp;

Written so that later, we like to understand, you said before we go back, if r4 is not const, then why can type type and object references must be consistent, you can think, if r4 is not constant, then allow assigned to r4, r4 also will change the value of the referenced object, but note, r4 binding is a temporary variable instead of the object d, so r4 can not change the value of d, so it seems r4 d references, but can not be used changes in the value of d, then it does not make sense, C ++ will be classified as illegal the behavior of this.

And references, can also make a pointer to a constant amount or very similar to the constant reference point for constant pointer can not change its value referents, and the pointer pointing to the constant nor predetermined objects to which it refers must be a constant. The so-called constant pointer pointing to the required value not only by changing the object pointer, but does not specify the value of the object can not be changed by other means.

const double pi=3.14; //pi是一个常量,它的值不能改变 double *ptr=&pi; //错误:ptr不能指向常量 const double *cptr = &pi; //正确 *cptr = 2.1; //错误,不能给*cptr赋值 double d = 3.14; cptr = &d; //正确 *cptr=4.1; //错误:不能直接修改*cptr的值 d=4.1 //正确,*cptr=4.1

The pointer itself is an object, so it can be placed before the * const pointer to explain itself is a constant, write mean, the same is that the value of the pointer itself, rather than pointing to:

int errNumb = 0; int c=1; int *const curErr = &errNumb; //curErr将一直指向errNumb,不能修改 curErr=&c; //错误,curErr不能修改 *curErr = c; //正确:curErr不能修改,但是*curErr可以修改 cout<<*curErr<<" "<<errNumb<<endl; //都等于1 const double pi = 3.14; double p=4.5; const double *const pip = &pi; //pip是一个指向常量对象的常量指针 pip=&p; //错误 *pip=p; //错误 const int ci=42; int bi = 32; const int *p2=&ci; //正确:p2指向ci const int *const p3=p2; //正确:p3和p2同时指向ci cout<<*p2<<" "<<*p3<<endl; //二者相等 p2=&bi; //p2指向bi,p3指向ci cout<<*p2<<" "<<*p3<<endl; //*p2=32,*p3=42 p2=p3; //正确 int &r=ci; //错误:普通int不能绑定到int常量上

Here teach you a method to determine the assignment statement to that above, assume that you can just look at the success of the assignment, then change the value of the left, the right of the target will be without errors, such as: p2 = p3, can not be changed * p2, but you can change p2, but change the value p2, the value of p3 is not affected, so you can p2 = p3, Why int & r = ci wrong, assuming that can be assigned successfully, then you can change the value of r to change the value of the ci, but ci is const type can not be changed, so the formula is a problem. Assignment statement like this can be so considered.

Guess you like

Origin www.cnblogs.com/maxwelldzl/p/11639410.html