Pointers and references look different enough (pointers use the “*
” and “->
” operators, references use “.
“), but they seem to do similar things. Both pointers and references let you refer to other objects indirectly. How, then, do you decide when to use one and not the other?
First, recognize that there is no such thing as a null reference. A reference must always refer to some object. As a result, if you have a variable whose purpose is to refer to another object, but it is possible that there might not be an object to refer to, you should make the variable a pointer, because then you can set it to null. On the other hand, if the variable must alwaysrefer to an object, i.e., if your design does not allow for the possibility that the variable is null, you should probably make the variable a reference.
“But wait,” you wonder, “what about underhandedness like this?”
- char *pc = 0;
- char& rc = *pc;
-
Well, this is evil, pure and simple. The results are undefined (compilers can generate output to do anything they like), and people who write this kind of code should be shunned until they agree to cease and desist. If you have to worry about things like this in your software, you’re probably best off avoiding references entirely. Either that or finding a better class of programmers to work with. We’ll henceforth ignore the possibility that a reference can be “null.”
Because a reference must refer to an object, C++ requires that references be initialized:
string& rs;
-
- string s("xyzzy");
- string& rs = s;
Pointers are subject to no such restriction:
The fact that there is no such thing as a null reference implies that it can be more efficient to use references than to use pointers. That’s because there’s no need to test the validity of a reference before using it:
void printDouble(const double& rd)
- {
- cout << rd;
- }
Pointers, on the other hand, should generally be tested against null:
- void printDouble(const double *pd)
- {
- if (pd) {
- cout << *pd;
- }
- }
Another important difference between pointers and references is that pointers may be reassigned to refer to different objects. A reference, however, always refers to the object with which it is initialized:
- string s1("Nancy");
- string s2("Clancy");
- string& rs = s1;
-
- string *ps = &s1;
-
- rs = s2;
-
-
-
- ps = &s2;
-
In general, you should use a pointer whenever you need to take into account the possibility that there’s nothing to refer to (in which case you can set the pointer to null) or whenever you need to be able to refer to different things at different times (in which case you can change where the pointer points). You should use a reference whenever you know there will always be an object to refer to and you also know that once you’re referring to that object, you’ll never want to refer to anything else.
References, then, are the feature of choice when you know you have something to refer to, when you’ll never want to refer to anything else, and when implementing operators whose syntactic requirements make the use of pointers undesirable. In all other cases, stick with pointers.