What is one benefit of declaring the parameter as a const reference instead of declaring it as a regular object?

Advertisement

int median(const my_array& a)

  • Actually, objects cannot 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.
  • The const qualifier Forbids the code to modify the argument, so the programmer can rest assured that the source object will remain unchanged.
  • The argument is passed as a reference, so the Function receives a copy that can be modified without affecting the original variable.

Note: This one is similar to Q6, but focuses on the const keyword.

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.