ThisArticleFrom more than tive C ++
In the past, I had no clear understanding of the differences between pointers and references. After reading this article, I had a deeper understanding of the differences between pointers and references. Save this article for future reading.
Pointers and references look completely different (pointers use the operators "*" and "->" and reference the operators "."), but they seem to have the same functionality. Both pointer and Reference allow you to indirectly reference other objects. How do you decide when to use pointers and when to use references?
First, you must realize that reference pointing to null values cannot be used under any circumstances. A reference must always point to some objects. Therefore, if you use a variable and direct it to an object, but the variable may not point to any object at some time, you should declare the variable as a pointer, in this way, you can assign a null value to the variable. On the contrary, if the variable must point to an object, for example, your design does not allow the variable to be empty, then you can declare the variable as a reference.
"But please wait," You asked with suspicion ."CodeWhat are the consequences ?"
Char * Pc = 0; // set the pointer to a null value
Char & rc = * PC; // point the reference to a null value
This is very harmful and there is no doubt. The result will be uncertain (the compiler can generate some output, which may lead to any issue ). Avoid writing such code unless they agree to correct the error. If you are worried that such code will appear in your software, you 'd better avoid using references completely, or else you can make betterProgramMembers. In the future, we will ignore the possibility that a reference points to a null value.
Because the reference will certainly point to an object, in C ++, the reference should be initialized.
String & RS; // error. The reference must be initialized.
String S ("Xyzzy ");
String & rs = s; // correct. RS points to S.
Pointers do not have such restrictions.
String * pS; // uninitialized pointer
// Valid but dangerous
The fact that there is no reference pointing to a null value means that the code to be referenced is more efficient than the pointer. Because you do not need to test its validity before using the reference.
Void printdouble (const double & rd)
{
Cout <RD; // No need to test Rd, it
} // Definitely points to a double value
Instead, pointers should always be tested to prevent being empty:
Void printdouble (const double * PD)
{
If (PD) {// check whether it is null
Cout <* PD;
}
}
Another important difference between a pointer and a reference is that a pointer can be re-assigned to point to another object. However, the reference always points to the specified object during initialization and cannot be changed later.
String S1 ("Nancy ");
String S2 ("Clancy ");
String & rs = S1; // Rs references S1
String * PS = & S1; // PS points to S1
Rs = S2; // Rs still references S1,
// But the value of S1 is
// "Clancy"
PS = & S2; // PS is now directed to S2;
// S1 has not changed
In general, you should use pointers in the following situations. First, you should consider the possibility of not pointing to any object (in this case, you can set the pointer to null ), second, you need to be able to point to different objects at different times (in this case, you can change the pointer ). If you always point to an object and it does not change its direction once it points to an object, you should use references.
Another case is that when you reload an operator, you should use references. The most common example is the operator []. A typical use of this operator is to return a target object, which can be assigned a value.
Vector <int> V (10); // create an integer vector (vector) with a size of 10;
// Vector is a template in the Standard C library (see section m35)
V [5] = 10; // the target object to be assigned is the value returned by the operator [].
If the operator [] returns a pointer, the next statement must be written as follows:
* V [5] = 10;
However, this will make V look like a vector pointer. Therefore, you will choose to let the operator return a reference. (This is an interesting exception. For details, refer to the m30 clause)
When you know that you must point to an object and do not want to change its direction, or when you overload operators and prevent unnecessary semantic misunderstanding, you should not use pointers. In other cases, the pointer should be used.