i bit confused 1 of examples in textbook. when string created, created type string. however, when same string passed function, function parameters of const string , not string.
here's part of code:
int main() { string str; cout << "blah blah.."; getline(cin, str); if (is_pal(str)) . . . } bool is_pal(const string& s) { . . . }
why function parameter const string& s instead of string& s? i've flipped on textbook can't seem find explanation =/ thanks.
objects may expensive copy, such std::string
, passed const lvalue reference in c++. common idiom; see everywhere. const lvalue reference can bind both lvalues , rvalues without making copies, efficient way pass strings functions not modify them.