11

Possible Duplicate:
What are the differences between pointer variable and reference variable in C++?

What does Class& mean in c++ and how is it different from Class*?

Class& foo;
Class* foo;
Community
  • 1
  • 1
user90450
  • 127
  • 1
  • 1
  • 3

7 Answers7

21

The & version represents a reference while the * version represents a pointer. The difference is far too big for a typical SO post. I suggest you start at the C++ FAQ lite

http://www.parashift.com/c++-faq-lite/references.html

I usually don't like to answer posts with a "you should use google" answer. However this is one topic that I highly advise you google. In particular google "c++ pointers vs. references". There is a wealth of information available on this topic and the discussions on those pages will trump anything we'll write here.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • 1
    I think Joel and Jeff would be pleased as punch if anyone using Google to answer this question got directed to a StackOverflow page and impressed by a StackOverflow advertiser. – Thomas L Holaday Apr 14 '09 at 00:25
  • 3
    Yeah I know this is old. But I've landed on this page after a google search for "difference between reference and pointer." It was result #3. – Ross Nov 23 '09 at 19:09
  • 2
    ugh, I googled it and came here... Literally the exact same question I have. – Jonathan. Jan 04 '13 at 19:14
6

The * is a pointer, the & is a reference. The difference between the two is that a pointer is an area of memory that must be dereferenced, eg. by means of the -> operator in order to be "seen" as a class instance. A reference is instead an "alias", just an alternative name for the same class instance. You don't need to use the -> operator with a reference. You use the dot operator.

Personally, I rarely used the references, mostly when I had a value object that I allocated on the stack. The new operator always returns a pointer, which you then have to dereference. Moreover, one of the most problematic issues of the references is that you cannot set them to NULL. In some cases, it is handy to have a function that accepts either an object pointer or NULL. If your function accepts a reference, you cannot pass a NULL (you could use the Null object pattern, however)

Stefano Borini
  • 138,652
  • 96
  • 297
  • 431
  • Of course, null pointers are a huge source of bugs in C++, so I don't think that references are bad in that regard. – Ed S. Apr 23 '09 at 20:39
5

A Class * can point at any class object, or none.

A Class & always points to exactly one class object, and can never point to a different one.

Furthermore, I believe Bjarne is a member of the set of people who have asserted "arrays in C are broken beyond repair," a Class * can point at a whole ding-dang array of class objects, lined up one after the other in memory, and there is absolutely no way in C to tell whether a Class * points at one or many.

Thomas L Holaday
  • 13,614
  • 6
  • 40
  • 51
0

Another difference is that reference variables must be initialized. You cannot create a reference variable like what is shown in the sample code. That would produce a compiler error.

zooropa
  • 3,929
  • 8
  • 39
  • 61
0

A reference (&) is just the same as a pointer (*), except that the C++ compiler ensures it not to be NULL. However, it can still be a dangling pointer (a pointer variable that has no reference such that it is garbage and invalid for any use).

Moshe Rabaev
  • 1,892
  • 16
  • 31
Dimitri C.
  • 21,861
  • 21
  • 85
  • 101
0

As stated you should google it, but to avoid misunderstanding:

  • References are NOT variables
  • References are NOT similar to pointers (but you can use them in a similar way)

Think of a Reference as a shortcut for the term that is assigned to it.

user80452
  • 23
  • 2
0

One additional tip that I would offer is the following:

Use references when you can, pointers when you have to. If the object is guaranteed to exist, you should probably use a reference. If it is not, then you probably have to use a pointer.

One additional advantage is that references remove ambiguity on ownership. As soon as a maintenance programmer sees a pointer, they'll start to wonder if they should delete it.

Check this example out:

// Wrapper class using a reference because the wrapped object always exists
class Wrapper
{
public:
  // If the wrapped is guaranteed to exist at creation, do it this way
  Wrapper(Wrapped& wrapped):_wrapped(wrapped) { /* empty */ }

  // put extra methods here.
  int getWrappedValue() const { return _wrapped.getValue(); }

private:
  Wrapped& _wrapped; // This object always exists and is valid
};

// Wrapper class written to support a possibly non-existent wrapped object.
class Wrapper
{
public:
  Wrapper(Wrapped* wrapped = 0):_wrapped(wrapped) { /* empty */

  void setWrappee(WRappee* wrapped) { _wrapped = wrapped; }

  int getWrappedValue() const; // Not making inline -- more complex

private:
  Wrapped* _wrapped; // Always check pointer before use
};

int Wrapper::getWrappedValue() const
{
  if (_wrapped)
  {
    return _wrapped->getValue();
  }
  else
  {
    return -1; // NOTE, this is a contrived example -- not getting into exceptions
  }
}