Why passing arguments as pointers instead of references?

#1

In many problems requiring inputs, such as 11.5 (compute the median of online data), the solutions use pointers, such as istringstream *. I think a reference (e.g., istringstream&) should be easier to use because we do not need to keep writing the asterisk in front of the variable.

Is there any specific reason for using a pointer rather than a reference in the argument lists of functions?

0 Likes

#2

It is considered the best practice in C++. Please take a look at
https://google.github.io/styleguide/cppguide.html#Reference_Arguments

There are other benefits for this, and mostly for readability as people can easily noticed which parameter will be modified while reading an invocation of a function without checking the definition of a function.

1 Like