The difference between the pointer and the reference

xiaoxiao2021-03-06  63

The pointer is completely different from the reference (pointer operator '*' and'-> ', reference to the use operator'. '), But they seem to have the same function. The pointers and references are all objects that let you indirectly reference other objects. How do you decide when using a pointer, when is it using reference?

First, we must realize that there is a reference to null values ​​without the case. A reference must always point to some objects. So if you use a variable and let it point to an object, this variable may not point some object at some point, then you should declare the variable as a pointer, because so you can give the value to the variable. Conversely, if the variable is definitely pointing to an object, for example, your design is not allowed to be empty, then you can declare the variable as a reference.

"But, please wait", you question, "What kind of consequences will this code?" Char * pc = 0; // Set pointer to null value char & rc = * pc; // Let the reference point to empty value

This is very harmful, no doubt. The results will be uncertain (the compiler can generate some outputs, resulting in anything possible), and people who should be avoided to write such code unless they agree to correct the error. If you are worried that such a code will appear in your software, then you should completely avoid the use of references, or let's go to make more excellent programmers. We will ignore the possibility of reference to null values.

Because references will definitely point to an object, in C, the reference should be initialized. String & rs; // Error, reference must be initialized String S ("XYZZY"); string & = s; // correct, RS points to s

The pointer does not have such a limit. String * ps; // Unin-initialized pointer // legal but dangerous does not exist that this fact means that this fact means that the code efficiency is high than using the pointer. Because you don't need to test its legitimacy before using the reference. Void PrintDouble (const Double & rd) {cout << rd; // does not need to test RD, it} // is definitely pointed to a Double value, pointer should always be tested to prevent it from empty: void printDouble (const doubleT) Pd) {if (pd) {// Checks if null cout << * pd;}}

Another important difference between the pointer and reference is that the pointer can be reassigned to point to another different object, and the reference is always directed to the object specified in initialization and cannot be changed.

String S1 ("Nancy"); String S2; String & RS = S1; // RS Reference S1 STRING * PS = & S1; // PS points to S1 RS = S2; // RS is still reference S1 // But The value of S1 is now "CLANCY" PS = & S2; // PS Now pointing to S2; // S1 does not change, in the following case you should use a pointer, one is that you consider the possibility of any object (In this case, you can set the pointer empty), and the other is that you need to point to different objects at different times (in this case, you can change the pointing point to the pointer). If you always point to an object and you will not change your pointing, you should use a reference.

There is also a situation, that is, when you overload an operator, you should use the reference. The most common example is operator []. This operator typically usage is to return a target object that can be assigned.

Vector v (10); // Establish a plastic vector (Vector), the size is 10 // is a template in a standard C library (see clause 35) v [5] = 10; // This is Assignment target object is the value returned by operator []

If the operator [] returns a pointer, then the latter statement must be written like this: * v [5] = 10;

But this will make V look like a vector pointer. So you will choose to return an operator back a reference. (This has an interesting exception, see Terms 30)

When you know that you have to point to an object and do not want to change its pointing, or when you overload the operator and to prevent unnecessary semantic misunderstandings, you should not use the pointer. And in addition to this other case, the pointer should be used.

转载请注明原文地址:https://www.9cbs.com/read-84201.html

New Post(0)