Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why pass by value and not by const reference?

Since const reference is pretty much the same as passing by value but without creating a copy (to my understanding). So is there a case where it is needed to create a copy of the variables (so we would need to use pass by value).

like image 249
Johnson Avatar asked Jan 25 '15 15:01

Johnson


1 Answers

  1. It's usually faster to pass basic data types such as ints, floats and pointers by value.
  2. Your function may want to modify the parameter locally, without altering the state of the variable passed in.
  3. C++11 introduces move semantics. To move an object into a function parameter, its type cannot be const reference.
like image 154
Neil Kirk Avatar answered Sep 22 '22 07:09

Neil Kirk