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?
A pointer in C and C++ programming is a variable that points to an address of another variable and not its value. When creating a pointer, use an asterisk (*); when determining the address of the variable, the ampersand (&), or the address-of operator, will display this value.
The & is a unary operator in C which returns the memory address of the passed operand. This is also known as address of operator. <> The * is a unary operator which returns the value of object pointed by a pointer variable. It is known as value of operator.
Put simply. & means the address-of, you will see that in placeholders for functions to modify the parameter variable as in C, parameter variables are passed by value, using the ampersand means to pass by reference. * means the dereference of a pointer variable, meaning to get the value of that pointer variable.
The ampersand is the address of operator. It returns the memory location of a variable and that's the only way it's used, prefixed to a variable like the engine on a train. The variable doesn't even have to be initialized, just declared.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With