1

First, I would like to clarify my question here. During my coding development, I found myself passing by reference in my class member functions except for those built-in types. But I have an idea in my mind why wouldn't I use a pointer to pass in an argument (passing by pointers).

Is it a bad practice to pass by reference not passing by pointers?

iRaySpace
  • 65
  • 8
  • And what about passing by value? –  Aug 05 '13 at 22:31
  • 4
    Why would you pass by pointer instead of by reference? To clutter the code with unnecessary *s everywhere? – chris Aug 05 '13 at 22:32
  • 1
    Passing pointers allows for nullptr, if that is necessary. And references do not allow reassignment. You can change the value of the thing they point to, but you cannot point them at a different object. – sbaker Aug 05 '13 at 23:35

1 Answers1

4

One common style is to pass input arguments by const reference, and output arguments by pointer. In this style, we don't pass output arguments by non-const reference to more clearly delineate input from output arguments.

seanmcl
  • 9,740
  • 3
  • 39
  • 45
  • 2
    Well, it's how code is written at Google, among other places. That is not an argument that it's a good idea. Only that it has been used effectively in large bodies of software. – seanmcl Aug 05 '13 at 22:37
  • @H2CO3 it's not brainless. It's quite common actually. It's useful when seeing `somefunction(&someobj);` because it's more obvious the object being passed in is going to be modified – Ryan Haining Aug 05 '13 at 22:40
  • 1
    @RyanHaining See why I prefer C to C++? (**You** said it was more obvious!) –  Aug 05 '13 at 22:41