可能重复:
Is it better in C++ to pass by value or pass by constant reference?
见这2个程序。
bool isShorter(const string s1, const string s2);
int main()
{
string s1 = "abc";
string s2 = "abcd";
cout << isShorter(s1,s2);
}
bool isShorter(const string s1, const string s2)
{
return s1.size() < s2.size();
}
和
bool isShorter(const string &s1, const string &s2);
int main()
{
string s1 = "abc";
string s2 = "abcd";
cout << isShorter(s1,s2);
}
bool isShorter(const string &s1, const string &s2)
{
return s1.size() < s2.size();
}
为什么第二个更好?