C++ functions: ampersand vs asterisk

Let's say you have a function that modifies a variable.

Should you write it like this: void myfunc(int *a) or like this void myfunc(int &a)?

The former forces you to call the function with myfunc(&b) so the caller is aware that b will be modified, but the latter is shorter and can be called simply with myfunc(b). So which is better to use? Is there something else I'm missing?


Pointers (ie. the '*') should be used where the passing "NULL" is meaningful. For example, you might use a NULL to represent that a particular object needs to be created, or that a particular action doesn't need to be taken. Or if it ever needs to be called from non-C++ code. (eg. for use in shared libraries)

eg. The libc function time_t time (time_t *result);

If result is not NULL, the current time will be stored. But if result is NULL, then no action is taken.

If the function that you're writing doesn't need to use NULL as a meaningful value then using references (ie. the '&') will probably be less confusing - assuming that is the convention that your project uses.


Whenever possible I use references over pointers. The reason for this is that it's a lot harder to screw up a reference than a pointer. People can always pass NULL to a pointer value but there is no such equivalent to a reference.

The only real downside is there reference parameters in C++ have a lack of call site documentation. Some people believe that makes it harder to understand code (and I agree to an extent). I usually define the following in my code and use it for fake call site documentation

#define byref
...
someFunc(byref x);

This of course doesn't enforce call site documentation. It just provides a very lame way of documenting it. I did some experimentation with a template which enforces call site documentation. This is more for fun than for actual production code though.

http://blogs.msdn.com/jaredpar/archive/2008/04/03/reference-values-in-c.aspx


I think I would disagre with @bb and @JaredPar and I lean to the opposite side of the fence. After years of trying to support other peoples C++ code, I often find problems lurking in non-obvious side effects of reference arguments. With C#, it is obvious since you have to prefix types of arguments with 'ref'/'out' but references are potentially confusing in C++. So, I like pointers because it's really clear something is coming back up. If you don't like points, C++ is not for you.


I come down on the pointer side of the fence, for reasons cited here and elsewhere. However, I will say that whatever you decide, you need to be consistent and document it in your style guide.

Google C++ style guide bans non-const reference arguments.


The former forces you to call the function with myfunc(&b) so the caller is aware that b will be modified

sometimes function could accept const pointer and caller will have wrong thinking that b will be modified.

My recommendation - prefer use references everywhere where it possible (ofcourse where it needed). In case with function argument - we get benefits:
- references can't be NULL - it help us to avoid errors and unnecessary asserts or checks.
- references have only one initialization point and in function boody you always know on what thing input parameter points.

I'm maintainer on large project. And in either cases I'm looking on function definition before call its. Ofcourse when I looking on function definition I see arguments definition by value, by reference, by const reference or by pointer.

But it seems like holy-war question, defferent peoples have different view on this point. E.g. google codding convension recomended use pointers in arguments which could be changed and allowed only const references: http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Reference_Arguments

All parameters passed by reference must be labeled const.