Is there any difference between these two usage?
1.
void Foo(const double &n)
{
cout<< "Hello: " << n << endl;
}
2.
void Foo(double n)
{
cout<< "Hello: " << n <<endl;
}
I’m looking for a general answer, not only for this context. (1. usage makes me confused.)
You can’t modify the value of the parameter in the first snippet, in the second one you can.
Outside the function, they both remain the same, because the second one passes by value.
For non-basic types, it makes a difference performance-wise. If you pass by reference (
1.), you pass the original object, not a copy. If you pass by value, a copy is created.