Distinguish between pointers and references in c++

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 always refer 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?”

1 & nbsp & nbsp & nbspchar

*pc = 0; & nbsp// set pointer to null
2 & nbsp & nbsp & nbspchar & rc = *pc; & nbsp // make reference refer to
3 & nbsp & nbsp & nbsp & nbsp & nbsp & nbsp & nbsp & nbsp & nbsp & nbsp // dereferenced null pointer

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:

1 & nbsp & nbsp & nbspstring & rs; & nbsp// error! References must
2 & nbsp & nbsp & nbsp & nbsp & nbsp & nbsp & nbsp & nbsp & nbsp & nbsp // be initialized
3 & nbsp & nbsp & nbspstring s(“xyzzy”);
4 & nbsp & nbsp & nbspstring & rs = s; & nbsp// okay, rs refers to s

Pointers are subject to no such restriction:

1 & nbsp & nbsp & nbspstring *ps; & nbsp// uninitialized pointer:
2 & nbsp & nbsp & nbsp & nbsp & nbsp & nbsp & nbsp & nbsp & nbsp & nbsp // valid but risky

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:

1 & nbsp & nbsp & nbspvoid printDouble(const double & rd)
2 & nbsp & nbsp & nbsp{
3 & nbsp & nbsp & nbsp & nbsp & nbsp & nbsp & nbsp & nbsp cout & lt & lt rd; & nbsp// no need to test rd; it
4 & nbsp & nbsp & nbsp} & nbsp // must refer to a double

Pointers, on the other hand, should generally be tested against null:

1 & nbsp & nbsp & nbspvoid printDouble(const double *pd)
2 & nbsp & nbsp & nbsp{
3 & nbsp & nbsp & nbsp & nbsp & nbsp & nbsp & nbsp & nbspif (pd) { // check for null pointer
4 & nbsp & nbsp & nbsp & nbsp & nbsp & nbsp & nbsp & nbsp & nbsp & nbsp & nbsp & nbsp & nbspcout & lt & lt *pd;
5 & nbsp & nbsp & nbsp & nbsp & nbsp & nbsp & nbsp & nbsp}
6 & nbsp & nbsp & nbsp}

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:

& nbsp1 & nbsp & nbsp & nbspstring s1(“Nancy”);
& nbsp2 & nbsp & nbsp & nbspstring s2(“Clancy”);
& nbsp3 & nbsp & nbsp & nbspstring & rs = s1; & nbsp // rs refers to s1
& nbsp4
& nbsp5 & nbsp & nbsp & nbspstring *ps = & s1; // ps points to s1
& nbsp6
& nbsp7 & nbsp & nbsp & nbsprs = s2; & nbsp // rs still refers to s1,
& nbsp8 & nbsp & nbsp & nbsp & nbsp & nbsp & nbsp & nbsp & nbsp & nbsp & nbsp // but s1’s value is now
9 // “Clancy”
10



Distinguish between pointers and references in c++