What’s a benefit of declaring the parameter as a const reference instead of declaring it as a regular object?

Advertisement

int median(const my_array& a);

  • The argument is passed as a reference, so the function receives a copy that can be modified without affecting the original value.
  • The argument is passed as a reference, so if the passed my_array object is large, the program will require less time and memory.
  • Actually objects can’t be passed as regular variables because they require a constructor call. Therefore a const reference is the only way to pass class instances to functions.
  • There are no benefits because a reference and an object are treated as the same thing.
Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for subscribing.

Something went wrong.

Leave a Comment

Share via
Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for subscribing.

Something went wrong.

Send this to a friend