I have found out the answer myself.
we have to put that const because string literals are const and in the code we were initializing a const string to a plain reference which is in error.
to make it clear look below:
string &r="some string";
is in error but
const string &r="some string";
is valid
and if :
string make_plural(string &word, size_t c,const string &ending = "s")
{
return c > 1 ? word + ending : word;
}
if the first parameter was a plain reference then the call could be:
string str = "thing";
cout << make_plural(str,2) << endl;
but if you want the call to be:
cout << make_plural("thing",2) << endl;
you have to add const for the first parameter as follows
string make_plural(const string &word, size_t cnt,const string &ending = "s")
{
return cnt > 1 ? word + ending : word;
}
reasons and rules to initializing parameter are the same as variables so:
plain references cannot be initialized by const values such as a string literals that are consts.