views:

375

answers:

5

Possible Duplicate:
Difference between pointer variable and reference variable in C++

When should I declare my variables as pointers vs objects passed-by-reference? They compile to the same thing in assembly (at least run-time asymptotically) so when should I use which?

void foo(obj* param)
void foo(obj& param)
+8  A: 

My rule is simple: use * when you want to show that value is optional and thus can be 0.

Excluding from the rule: all the _obj_s around are stored in containers and you don't want to make your code look ugly by using everywhere foo(*value); instead of foo(value); So then to show that value can't be 0 put assert(value); at the function begin.

Mykola Golubyev
+2  A: 

One reason to use pointers is if it makes sense to pass a NULL value into the function. With a pointer, it's expected to be able to do this. With a reference, it is not expected to be able to do this.

(However, by doing tricky things it is still possible to pass a NULL into a reference parameter. You can expect that the called function may crash in this case.)

Another convention is that if you pass a pointer into a function, the function may use the pointer to take ownership of the object (especially in a COM-like reference counted environment). If you pass a reference, then the called function can expect to use the object for the duration of the function call but not to keep a pointer to the object for use later.

Greg Hewgill
+2  A: 

I follow the Google style guide standard as it makes the most sense to me. It states:

Within function parameter lists all references must be const:

void Foo(const string &in, string *out);

In fact it is a very strong convention in Google code that input arguments are values or const references while output arguments are pointers. Input parameters may be const pointers, but we never allow non-const reference parameters.

One case when you might want an input parameter to be a const pointer is if you want to emphasize that the argument is not copied, so it must exist for the lifetime of the object; it is usually best to document this in comments as well. STL adapters such as bind2nd and mem_fun do not permit reference parameters, so you must declare functions with pointer parameters in these cases, too.

MahlerFive
I do not agree with this point. This is misuse of reference/pointer concept in the language. References are their to ensure at compile-time that a value is initialized. This is a very strong contract where one states the function accepts only initialized values. Languages like Java or C# have lack, which forces developers additionaly to check if the value being passed is NULL (null) or not. I don' see enforcement in the contract that I am allowed to accept only const values.
ovanes
+1  A: 

The other difference between pointers and references is that it is implied that you won't hold on to a reference, unless you pass one to a constructor. Passing pointers may mean that an object might hold onto it for a while, like a composite pattern object.

quamrana
A: 

A good reason why I use pointers and not references in my C++ applications, is readability. When using a pointer you see what is really happening, and when using pointers the syntax tells you what's really happening.

Same goes for "0" or "NULL". I prefer using "NULL", this way 3 months later when I see a code that looks like:

somevar_1 = 0;
somevar_2 = NULL;

I know that somevar_1 is an int (or float) and somevar_2 is some kind of pointer.

elcuco