C++ pointer and reference distinction and use instructions

  • 2020-04-01 21:34:14
  • OfStack

Pointers and references look completely different (Pointers with the operators "*" and "->"). , using the operator ". "), but they seem to have the same function. Pointers and references both allow you to indirectly refer to other objects. How do you decide when to use Pointers and when to use references?

First, realize that you should never use a reference to a null value under any circumstances. A reference must always point to some object. So if you take a variable and make it point to an object, but the variable may not point to any object at some point, you should declare the variable as a pointer, because then you can assign a null value to the variable. Conversely, if the variable definitely points to an object, such as if your design does not allow the variable to be empty, you can declare the variable as a reference.

"But wait a minute," you ask incredulously. "what are the consequences of this code?"
 
char *pc = 0; //Set the pointer to null
char& rc = *pc; //Let the reference point to a null value

It is very harmful, no doubt about it. The result will be uncertain (the compiler can produce some output that makes anything possible). People who write such code should be avoided unless they agree to correct it. If you're worried that such code will end up in your software, you'd better avoid references altogether, or let better programmers do it. We will later ignore the possibility that a reference points to a null value.
Because the reference will definitely 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 have no such limitation.
 
string *ps; //Uninitialized pointer
//Legal but dangerous

The fact that there are no references to null values means that code using references is more efficient than using Pointers. Because you don't need to test the validity of a reference before you use it.
 
void printDouble(const double& rd) 
{ 
cout << rd; //You don't need to test rd, it definitely points to a double value
} 

Instead, Pointers should always be tested to prevent them from being null:
 
void printDouble(const double *pd) 
{ 
if (pd) //Check for NULL
{ 
cout << *pd; 
} 
} 

Another important difference between a pointer and a reference is that a pointer can be reassigned to point to a different object. But the reference always points to the object specified at the time of initialization and cannot be changed later.
 
string s1("Nancy"); 
string s2("Clancy"); 
string& rs = s1; //Rs reference s1
string *ps = &s1; //Ps to s1
rs = s2; //Rs still references s1, but the value of s1 is now "Clancy"
ps = &s2; //Ps is now pointing to s2;
//S1 has not changed

In general, you should use Pointers when you consider the possibility of not pointing to any object (in which case you can set the pointer to null) and when you need to be able to point to different objects at different times (in which case you can change the pointer). If you always point to an object and don't change the point once you point to an object, you should use a reference.

Another case is when you overload an operator, you should use a reference. The most common example is the operator []. This operator is typically used to return a target object that can be assigned a value.
 
vector<int> v(10); //Set up a vector with a size of 10;
v[5] = 10; //The target object to be assigned is the value returned by the operator []

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

But that makes v look like a vector pointer. So you choose to have the operator return a reference.

You shouldn't use Pointers when you know you have to point to an object and don't want to change its pointing, or when overloading the operators and preventing unnecessary semantic misunderstandings. In other cases, Pointers should be used.

Related articles: