0

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

Both performance-wise and functionality-wise?

Which means, if you want to modifiy the contents of some data pointed by some pointer during a function call, passing pointer arguments works exactly the same as passing reference arguments?

Community
  • 1
  • 1
60080
  • 217
  • 3
  • 9

1 Answers1

2

Performance-wise, references perform slightly better than pointers. That has to do with pointer adjustment needed in certain cases, for instance when multiple inheritance is in place. When the converted pointer is null, the adjustment has to be reversed (or not done at all) so that the pointer remains null. The fact that references cannot be null mean that an extra check is not needed.

Functionality-wise, pointers can be null while references can´t, and pointers can be reassigned while references can´t. Other than the basic different syntax to access them.

So basically no, they are not exactly the same thing.

K-ballo
  • 80,396
  • 20
  • 159
  • 169
  • pointer adjustments are not only performed with multiple inheritance. e.g. if `Base` is non-polymorphic and `Derived` has a virtual method and is derived from `Base`, then casting between these types may in practice perform an adjustment. and it does with visual c++. just to be clear, i didn't downvote your answer. but please fix, because the mention of MI is pretty misleading (and unnecessary)! ;-) – Cheers and hth. - Alf Dec 29 '12 at 02:48
  • @Cheersandhth.-Alf: Good point, more scenarios where references are slightly better performance-wise then... – K-ballo Dec 29 '12 at 02:49
  • @Cheersandhth.-Alf: Would you mind editing the answer yourself? I need to do some more research in light of the point you just raised until I fully understand it. – K-ballo Dec 29 '12 at 02:50
  • @Cheers and hth. - Alf: I see what you mean, not even a `static_cast` is needed. – K-ballo Dec 29 '12 at 03:12