开发者

Difference between pass-by-reference & and *? [duplicate]

This question already has answers here: Passing a modifiable parameter to c++ function (12 answers) Closed 11 months ago.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜