C++函数使用 传参原则

Yes. The same reason if you only ever read an argument you make the parameter const&.

T        // I'm copying this
T&       // I'm modifying this
const T& // I'm reading this

Those are your "defaults". When T is a fundamental type (built-in), though, you generally just revert to const T (no reference) for reading, because a copy is cheaper than aliasing.

for(auto i : vec){
   std::cout << i << std::endl;
}
for(auto &i : vec){
   std::cout << i << std::endl;
}
for(const auto &i : vec){
   std::cout << i << std::endl;
}
posted @ 2020-02-14 10:43  zhanglinfengNEPDI  阅读(147)  评论(0)    收藏  举报