More Effective C ++ Terms

zhaozj2021-02-08  421

Terms 1: The difference between pointer and reference

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 wait a minute", you question, "what kind of consequences will this code?"

Char * pc = 0; // Setting pointer is null

Char & rc = * pc; // Let the reference point to null 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

This fact does not exist that this fact means that the code efficiency is high than the use of pointers. 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

} // Faster points to a double value

Instead, the pointer should always be tested and prevent it from:

Void PrintDouble (const Double * Pd)

{

IF (PD) {// Check if NULL

Cout << * pd;

}

}

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

String S1 ("Nancy");

String S2 ("CLANcy");

String & RS = S1; // RS reference S1

String * ps = & s1; // ps pointing to S1

RS = S2; // RS still reference S1,

/ / But the value of S1 is now

// "CLANCY"

PS = & S2; // ps Now points to S2;

// S1 has not changed

In general, in the following cases you should use a pointer, one is that you consider that there is no possibility of any object (in this case, you can set the pointer empty), the other is that you need to be able to be able to be at different times. Point different objects (in this case, you can change the pointer pointing). 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), size is 10;

// Vector is a template in a standard C library (see clause 35)

v [5] = 10; // This assigned target object is the value returned by the operator []

If the operator [] returns a pointer, then the latter statement has to write 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-963.html

New Post(0)