What is the difference between passing-by-reference and using the C pointer notation?
void some_function(some_type& param)
and
void some_function(some_type *param)
Thanks
What is the difference between passing-by-reference and using the C pointer notation?
void some_function(some_type& param)
and
void some_function(some_type *param)
Thanks
Basically you handle a safe pointer as if it was your own object.
Syntactic sugar.
When you pass a pointer to a variable in a subroutine call, the address of that variable is passed to the subroutine. To access the variable in the subroutine, the pointer has to be dereferenced.
When you pass a reference to a variable, the compiler takes care of obtaining the address of the variable when the variable is passed to the subroutine and dereferencing the variable in the subroutine.
A pointer can be repointed to a new object, but a reference cannot.
void MyFunction (MyType *myParameter)
{
myParameter = new MyType ("BoogaBoogaBooga");
}
void main ()
{
MyType *myArgument = new MyType ("WonkaWonkaWonka");
MyType *myCopy = myArgument;
MyFunction (myArgument);
//myArgument is now pointing to the BoogaBoogaBooga object, but
//myCopy is still pointing to the WonkaWonkaWonka object
}
Also, as others have already mentioned, a null pointer could be passed in, but a null reference could not be. Calling by reference provides nice syntactic sugar for accessing the provided object's members.