1

I'm having a problem with a class like this:

class Sprite {
    ...
        bool checkCollision(Sprite &spr);
    ...
};

So, if I have that class, I can do this:

ball.checkCollision(bar1);

But if I change the class to this:

class Sprite {
    ...
        bool checkCollision(Sprite* spr);
    ...
};

I have to do this:

ball.checkCollision(&bar1);

So, what's the difference?? It's better a way instead other?

Thank you.

Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
Kactung
  • 690
  • 2
  • 8
  • 21
  • See http://stackoverflow.com/questions/3063514/what-does-it-mean-to-pass-a-variable-to-a-function-e-g-string-insert-size posted a bit earlier. –  Jun 17 '10 at 21:42

4 Answers4

4

In both cases you are actually passing the address of bar1 (and you're not copying the value), since both pointers (Sprite *) and references (Sprite &) have reference semantics, in the first case explicit (you have to explicitly dereference the pointer to manipulate the pointed object, and you have to explicitly pass the address of the object to a pointer parameter), in the second case implicit (when you manipulate a reference it's as if you're manipulating the object itself, so they have value syntax, and the caller's code doesn't explicitly pass a pointer using the & operator).

So, the big difference between pointers and references is on what you can do on the pointer/reference variable: pointer variables themselves can be modified, so they may be changed to point to something else, can be NULLed, incremented, decremented, etc, so there's a strong separation between activities on the pointer (that you access directly with the variable name) and on the object that it points to (that you access with the * operator - or, if you want to access to the members, with the -> shortcut).

References, instead, aim to be just an alias to the object they point to, and do not allow changes to the reference itself: you initialize them with the object they refer to, and then they act as if they were such object for their whole life.

In general, in C++ references are preferred over pointers, for the motivations I said and for some other that you can find in the appropriate section of C++ FAQ.

In terms of performance, they should be the same, because a reference is actually a pointer in disguise; still, there may be some corner case in which the compiler may optimize more when the code uses a reference instead of a pointer, because references are guaranteed not to change the address they hide (i.e., from the beginning to the end of their life they always point to the same object), so in some strange case you may gain something in performance using references, but, again, the point of using references is about good programming style and readability, not performance.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
  • 1
    References and pointers do NOT have the same semantics. Consider assignment, for example. –  Jun 17 '10 at 21:45
  • Thank you, so I will use then Sprite* spr; cause I see more clearly. – Kactung Jun 17 '10 at 21:50
  • @Puyover I don't think anyone is suggesting you should do that - you should normally prefer references. –  Jun 17 '10 at 21:56
  • @Puyover: Er... the whole point of my answer was to suggest to use references instead of pointers... <_< As stated here (http://www.parashift.com/c++-faq-lite/references.html#faq-8.6), "Use references when you can, and pointers when you have to." – Matteo Italia Jun 17 '10 at 21:57
  • Oh ok. Sorry but I don't understand English very well, so I understood the opposite xD Thanks again! – Kactung Jun 18 '10 at 03:01
1

A reference cannot be null. A pointer can.

If you don't want to allow passing null pointers into your function then use a reference.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • Oh ok, but otherwise, one way is faster than the other or more efficient? – Kactung Jun 17 '10 at 21:42
  • If you pass a pointer and you want to write robust code then you'll have to check it for null. If you use a reference you don't need to make that check. – Mark Byers Jun 17 '10 at 21:48
  • 2
    @Puyover: Speed should be your last concern. Learning the language is much more important. – GManNickG Jun 17 '10 at 22:05
0

They both do the (essentially) same thing - they pass an object to a function by reference so that only the address of the object is copied. This is efficient and means the function can change the object.

In the simple case you give they are equivalent.

Main differences are that the reference cannot be null, so you don't have to test for null in the function - but you also cannot pass a null object if the case of no object is valid.

Some people also dislike the pass by reference version because it is not obvious in the calling code that the object you pass in might be modified. Some coding standards recommend you only pass const references to functions.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
0

With the pointer you need to specifically let the compiler know you want to pass the address of the object, with a reference, the compiler already knows you want the ptr. Both are ok, it's a matter of taste, I personally don't like references because I like to see whats going on but thats just me.

Steve Sheldon
  • 6,421
  • 3
  • 31
  • 35
  • 2
    There are places in C++ where it is not a matter of taste - only a reference will do. For example, as a parameter to a copy constructor. –  Jun 17 '10 at 21:52