Difference between pass-by-reference & and *? [duplicate]
What is the difference between passing-by-reference and usi开发者_StackOverflow社区ng the C pointer notation?
void some_function(some_type& param)
and
void some_function(some_type *param)
Thanks
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.
- You can't get a NULL reference: this alone gives you lots of safety
- You can treat your reference as if it was an object: you can dereference it or whatever you need.
Basically you handle a safe pointer as if it was your own object.
精彩评论