const string& 相对 const string有哪些优势? #21
cherishman2005
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
传参时 const string& 相对 const string 有哪些优势?
比如 C++ Primer 中的这段代码,为什么要用 const string& s 而非 const string s,这二者有何差别?
& 的目的是引用,避免再了复制一个std::string
const 是为了限定它只读
const string s 的话还是要再复制一次岂不是很浪费,既然已经是只读了为啥不直接用引用。
https://www.zhihu.com/question/37153228
Beta Was this translation helpful? Give feedback.
All reactions