Difference between & and * as a parameter
What is the difference between the following two parameter types? The first accepts a pointer, which is in effect a memory address, and the second is also a memory address?
foo(float& bar)
{
// do stuff
}
foo(float* bar)
{
// do stuff
}
Could you not call both with:
float pow = 3.0f;
foo(&pow);
开发者_开发知识库
or
float* pow = 3.0f;
foo(pow);
A pointer can be
NULL
, while a reference can't. This can be useful if you need to pass aNULL
object for whatever reason.With the pointer syntax, you pass a pointer when you call the function. With references, you just pass the variable:
refer(float& bar) {} point(float* bar) {} float afloat = 1.0f; refer(afloat); point(&afloat);
This means with the pointer syntax you have to pass a pointer when you call the function. With the reference syntax, you don't know if the function takes it by reference or by value without looking at the function definition.
With the reference syntax you don't have to dereference the pointer in your function, and work with it more naturally in your
// do stuff
section.foo(float& bar) { bar = 3.0f; } // versus foo(float* bar) { *bar = 3.0f; }
No, they are not the same. The first is taking a parameter by reference and would be called like this:
float pow = 3.0f;
foo(pow) // foo can change the value of pow!
the second accepts a pointer and could be called by either of your examples (both of which are passing a pointer, not a reference).
NOTE: your second example, while it passes a float*
does not properly initialize the pow
variale, and therefore won't compile. Instead, something like this would work:
float *pow = new float(3.0);
foo(pow);
delete pow;
While references have similarities to pointers, it is not mandated that they are implemented internally by pointers. For example, often the compiler can inline calls and just modify the argument directly, no pointer passed in that case.
In general, think of a reference as "just another name" for a variable. For example:
Person Samuel_Clemens;
Person &Mark_Twain(Samuel_Clemens); // just another name
The difference is that the first cannot receive a null pointer, while the second can. But with a bit of effort you can make the first receive null pointer too:
float *a = null;
pow(*a);
Edit: All the following proved to be wrong, I'll keep it as reference for the comments:
The difference is that the reference version will throw an exception when dereferencing a null reference while pointer version will just segfault:
float *a = null;
float &b = *a; // works... somehow?
b = 1; // throws exception
*a = 1; // segmentation fault
float*
is a pointer to a float number, whereas float&
is a reference to a float number.
With a pointer, you can say function(null)
letting the argument point to null
(which represents nothing in particular, and often causes undefined behaviour (=crash)). A reference can't reference to nothing (at least not that easy).
When using float*
, you will always treat this argument as a pointer, and the compiler does as well. When you use float&
, you can treat it as a "normal" (i.e. non-pointer) variable, but it is as if you were using a pointer.
In the first (reference), you are interested on reading and writing the original variable. In the secondth (pointer), you are interested on receiving the address of the original variable.
The difference is mostly taste, except the fact that the pointer version allows you to not-pass any value using a NULL value.
精彩评论