I have been following this tutorial and I can't understand this section.
So it says that pass by value creates copy of the data so that can be ineffiecient, since it creates copy of the data, makes sense:
string concatenate (string a, string b)
{
return a+b;
}
then you have this, to 'solve the creating copies of data' issue:
string concatenate (string& a, string& b)
{
return a+b;
}
which makes sense since it is using references.
But I do not get this:
string concatenate (const string& a, const string& b)
{
return a+b;
}
Which says:
By qualifying them as const, the function is forbidden to modify the values of neither a nor b, but can actually access their values as references (aliases of the arguments), without having to make actual copies of the strings.
Why just not use the second way? In this example no modification of data is being done anyway? Can somebody give me a better example ?
Is it just done for safety reasons?