有几个需要注意的地方:
1.返回引用
例如:
const string &shorterString(const string &s1, const string &s2)
{
return s1.size() < s2.size() ? s1 : s2;
}
形参和返回类型都是指向const对象的引用,调用函数和返回结果时,都没有复制这些string对象。
试着运行了一下,果然运行时出错,而编译时就不会报错的。
其实平时很少会定义引用类型的函数,翻了下书还是没有明白为什么,以后补充。