Question

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)
Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top