首页 > 编程语言 > 详细

C++函数使用 传参原则

时间:2020-02-14 11:35:25      阅读:54      评论:0      收藏:0      [点我收藏+]

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;
}

C++函数使用 传参原则

原文:https://www.cnblogs.com/zhanglinfengnepdi/p/12306513.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!