views:

988

answers:

8

Are these the same:

int foo(bar* p) {
  return p->someInt();
}

and

int foo(bar& r) {
  return r.someInt();
}

Ignore the null pointer potential. Are these two functions functionally identical no matter if someInt is virtual or if they are passed a bar or a subclass of bar?

Does this slice anything:

bar& ref = *ptr_to_bar;

-cory

A: 

I haven't used C++ in a long time, so I'm not even going to attempt to really answer your question (sorry); However, Eric Lippert just posted an excellent article about pointers/references that I figured I'd point you to.

Chris Shaffer
+9  A: 

C++ references are intentionally not specified in the standard to be implemented using pointers. A references is more like a "synonym" to a variable than a pointer to it. This semantics opens some possible optimizations for the compiler when it's possible to realize that a pointer would be an overkill in some situations.

A few more differences:

  • you can't assign NULL to a reference. this is a crucial difference and the main reason you'd prefer one over the other.
  • When you take the address of a pointer, you get the address of the pointer variable. when you take the address of a reference, you get the address of the variable being referred to.
  • You can't reassign a reference. once it is initialized it points to the same object for its entire life.
shoosh
You actually can assign NULL to a reference IF you cast it to a pointer of the type and then dereference the pointe.g. int and you can also reassign a reference if you place it within a union and change the corresponding value in the union.
Grant Peters
And i should add that these should never be done because when a function asks for a reference, it doesn't ever expect NULL, and although most references are basically implemented the same way as pointers, this MAY not be the case in all scenarios/platforms.
Grant Peters
if you're assigning to a corresponding value of a union then you're not really assigning to a reference are you?
shoosh
+2  A: 

Yes they are functionally identical. Since a reference will require you to set it to an object before using it, you wont have to deal with null-pointers or pointers to invalid memory.

It is also important to see the semantical difference:

  • Use a reference when you would actually pass the object normal - but it is so large that it makes more sense to pass a reference to the object rather than making a copy (if you are not modifying the object that is).
  • Use a pointer when you want to deal with the memory address rather than with the object.
Patrick Daryll Glandien
A: 

The functions are obviously not "the same", but with regard to virtual behaviour they will behave similarly. Regarding slicing, this only happens when you deal withvalues, not references or pointers.

anon
Actually, they probably generate identical machine code so in that sense they are the same.
jmucchiello
+3  A: 

Ignoring every syntactic sugar and possibilities that can be done with the one and not with the other and difference between pointers and references explained in other answers (to other questions) ... Yeah those two are functionally exactly the same! Both call the function and both handle virtual functions equally well.

And no, your line does not slice. It's just binding the reference directly to the object pointed to by a pointer.

Some questions on why you would want to use one over the other:

Instead of trying to come up with differences myself, i delegate you to those in case you want to know.

Johannes Schaub - litb
A: 

As everyone else has mentioned, in implementation references and pointers are largely the same. There are some minor caveats:

  • You can't assign NULL to a reference (shoosh mentioned this): that's significant since there is no "undefined" or "invalid" reference value.

  • You can pass a temporary variable as a const reference, but it's not legal to pass a pointer to a temporary.

For example, this is okay:

class Thingy; // assume a constructor Thingy(int,int)
void foo(const Thingy &a)
{ 
   a.DoSomething();
}

void bar( ) 
{
  foo( Thingy(1,2) );
}

but most compilers will complain about

void foo2( Thingy * a);

void bar2()
{
  foo( &Thingy(1,2) );
}
  • Taking the address of a variable to get a pointer forces the compiler to save it to memory. Assigning a reference to a local variable just creates a synonym; in some cases this may allow the compiler to keep the data on the register and avoid a load-hit-store. However, this only applies to local variables -- once something is passed as a parameter by reference, there's no avoiding saving it to stack.

 

void foo()
{
   int a = 5;
   // this may be slightly more efficient
   int &b = a;
   printf( "%d", ++b );
   // than this
   int *c = &a;
   printf( "%d", ++(*c) );
}
  • Similarly, the __restrict keyword cannot be applied to references, only pointers.

  • You can't do pointer arithmetic with references, so whereas if you have a pointer into an array then the next element in the array can be had through p+1, a reference only ever points at one thing in its entire life.

Crashworks
+1  A: 

Not sure if anyone answered your 2nd question hidden at the bottom about slicing... no that won't cause slicing.

Slicing is when a derived object is assigned (copied) to a base class object -- the derived class's specialization is "sliced" off. Note that I said the object is copied, we're not talking about pointers being copied/assigned, but the objects themselves.

In your example, that's not happening. You're just de-referencing a pointer to a Bar object (thereby resulting in a Bar object) being used as the rvalue in a reference initialization. Not sure I got my terminology right...

Dan
+1  A: 

Reference is a constant pointer i.e., you can't change the reference to refer to other object. If you change, value of the referring object changes.

For Ex:

       int j = 10;
       int &i = j;
       int l = 20;
       i = l; // Now value of j = 20

       int *k = &j;
       k = &l;   // Value of j is still 10
Vinay