0

Possible Duplicate:
What are the differences between pointer variable and reference variable in C++?
When to pass by reference and when to pass by pointer in C++?

Seems to me that a pointer is more flexible and basically the same as a reference. References can't be null, but what happens if you have a reference of a pointer that you then make null? Doesn't that case then negate the 'can't be null' advantage?. And is it really an advantage anyway?

Why not just always use pointers? (I'm genuinely asking, I honestly haven't made up my mind on this)

Community
  • 1
  • 1
Dollarslice
  • 8,654
  • 20
  • 52
  • 80
  • There are some circumstances to use pointers, and some to use references. Which circumstances are you interested in? – Benjamin Lindley Mar 22 '12 at 15:19
  • possible duplicate of [When to pass by reference and when to pass by pointer in C++?](http://stackoverflow.com/questions/3613065/when-to-pass-by-reference-and-when-to-pass-by-pointer-in-c) or http://stackoverflow.com/questions/57483/what-are-the-differences-between-pointer-variable-and-reference-variable-in-c or ... any of the tons of others. – Brian Roach Mar 22 '12 at 15:20
  • I did think this had probably been asked before, but I couldn't find one asking this exact question. – Dollarslice Mar 22 '12 at 15:45

7 Answers7

1

By using a reference you are explicitly stating that it cannot be 0. It makes sense when some value in code cannot be uninitialised. On the other hand pointers are useful when value can be either initialised or uninitialised. You can check it by comparing to 0.

ks1322
  • 29,461
  • 12
  • 91
  • 140
1
  • A reference cannot be null, as you said.

  • A reference is always initialized. A non-null pointer can still be not initialized at all.

  • A reference is stable. You cannot make the reference point first to A, and then to B. Once initialized, it is an alias of one and the same object

  • The syntax of references is somewhat neater.

Armen Tsirunyan
  • 120,726
  • 52
  • 304
  • 418
1

A reference to a pointer that can be null is not the same as a reference to the actual value or object. The pointer's value can be null, but the actual pointer is not!

Using references gives you peace of mind knowing that you are safe and weird stuff won't happen. So when you don't need the features of actual pointers, you should always use references. In general (and there are always exceptions) use pointers only if:

a. you need dynamic allocation of memory during execution.

b. you actually need pointer arithmetic (p++)

c. you need to save allocated objects inside containers, but in this case it's a generally good practice to wrap your pointers with something like shared_ptr.

Not_a_Golfer
  • 40,006
  • 7
  • 115
  • 81
1

The flexibility of pointers comes with a price: every time you see one, you have to consider that you may be using that flexibility somewhere. That's more to have to watch out for...more to debug..etc. References, being simpler and less featurish, carry less mental baggage.

Add to that, the syntax is less cluttered with *s and ->s. :)

cHao
  • 78,897
  • 19
  • 136
  • 168
1

When in doubt, go to the source (Stroustrup): http://www2.research.att.com/~bs/bs_faq2.html#pointers-and-references

References are good for operator overloading and copy constructors.

References are guaranteed to not be null and to be initialized.

Using pointers as arguments works well when you want to have arguments that are optional or that can be ignored when the value is NULL.

Joseph Willcoxson
  • 5,095
  • 1
  • 14
  • 23
0

Dereferencing a null pointer to create a reference will result in undefined behavior.

See also Is there a platform or situation where dereferencing (but not using) a null pointer to make a null reference will behave badly?

and What are the differences between a pointer variable and a reference variable in C++?

Community
  • 1
  • 1
Mark Ransom
  • 271,357
  • 39
  • 345
  • 578
0

Usually, references are just syntactic sugar of pointers which means whereever you use one, can be used other as well (well, not always, though). But sometimes, references make more sense than pointers, especially when you define a function whose argument(s) cannot be null, or checking for null argument is that elegant. In such cases, you simply make them references. One such example is swap function:

 template<typename T>
 void swap(T & a, T & b)
 {
       T temp(a);
       a = b;
       b = temp;
 }

If one uses pointers, then it would become this:

 template<typename T>
 void swap(T * a, T * b)
 {
       //do you want to check for null here?
       T temp(*a);
       *a = *b;
       *b = temp;
 }

I think, most C++ programmers will find the first solution using reference elegant, and the second solution unnecessarily cumbersome.

Nawaz
  • 327,095
  • 105
  • 629
  • 812
  • "Sometimes" references make more sense than pointers? I'd rather say, "most of the time". – leftaroundabout Mar 22 '12 at 15:45
  • @leftaroundabout: I think, most of the time, it is just syntactic sugar but then sugar also matters, as without it life is very bitter, so I kinda of agree with you. – Nawaz Mar 22 '12 at 15:52