views:

975

answers:

7

duplicate:
Difference 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;
+16  A: 

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
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
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
+5  A: 

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
+3  A: 

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
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 Swangren
A: 

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
A: 

A reference (&) is just the same as a pointer (*), except that the C++ compiler ensures it not to be NULL. It can still be a dangling pointer, however.

Dimitri C.
A: 

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.

A: