\" and references use operators \".\")," />
First of all, realize that under no circumstances can you use null references. References must always point to an object. So if you use a variable and point it to an object, but the variable may not point to any object at some time, you should declare the variable as a pointer, because then you can assign a null value to the variable. Conversely, if the variable explicitly points 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 a moment," you asked doubtfully. "What will happen to such code?"
char * PC = 0; //Set the pointer to null.
Char & amprc = * pc// Let the reference point to a null value.
This is very harmful, no doubt. The result will be uncertain (the compiler can produce some output, so anything can happen). People who write this code should be avoided unless they agree to correct their mistakes. If you are worried that such code will appear in your software, then you'd better avoid using references completely or let a better programmer do it. We will ignore the possibility that references will point to null values in the future.
Because the reference will definitely point to an object, in C++, the reference should be initialized.
Chord and chord. RS; //Error, reference must be initialized.
strings(" xyzzy ");
Chord and chord. RS = s; //correct, rs points to S.
Pointers are not so limited.
String * ps// uninitialized pointer
//legal but dangerous
The fact that there are no references to null values means that code that uses references is more efficient than code that uses pointers. Because you don't need to test the validity of a reference before using it.
void print double(const double & amp; R&D)
{
cout & lt& ltrd; //There is no need to test rd, it
}//Definitely points to a double value.
Instead, you should always test pointers to prevent them from being empty:
voidprintDouble(constdouble*pd)
{
If(pd){// Check whether it is empty.
cout & lt& lt* pd
}
}
Another important difference between pointers and references is that pointers can be reassigned to point to different objects. However, the reference always points to the object specified at initialization and cannot be changed later.
strings 1(" Nancy ");
strings 2(" Clancy ");
Chord and chord. RS = s 1; //rs references s 1.
string * ps = & amps 1; //ps points to s 1.
Rs = s2//rs or s 1.
//But now the value of s 1
//"Clancy"
ps = & ampS2; //ps now points to S2;
//s 1 has not changed.
Generally speaking, you should use the pointer 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 be empty); Secondly, you need to be able to point to different objects at different times (in this case, you can change the direction of the pointer). If you always point to an object, once you point to an object, you won't change this point, then you should use a reference.
In another case, when an operator is overloaded, a reference should be used. The most common example is the operator []. The typical use of this operator is to return a target object that can be assigned a value.
Vector & LTINT & GTV (10); //Establish a shaping vector with the size of 10;
//Vector is a template in the standard C library (see clause M35).
v[5]= 10; //The target object of assignment is the value returned by the operator [].
If the operator [] returns a pointer, the following statement must be written as follows:
* v[5]= 10;
But this will make v look like a vector pointer. So you will choose to let the operator return a reference. (There is an interesting exception, see article M30)
You should not use pointers when you know that you must point to an object and don't want to change its pointing, or when overloading operators to prevent unnecessary semantic misunderstanding. In other cases, pointers should be used.