加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 通过const引用或值传递int,有什么区别?

发布时间:2020-12-16 03:33:24 所属栏目:百科 来源:网络整理
导读:参见英文答案 Is it counter-productive to pass primitive types by reference? 7个 当我将诸如int和double之类的原语传递给函数时,最好是通过const引用或值传递它们(假设我不更改变量的值)? int getValueFromArray(int index){ // return the value from
参见英文答案 > Is it counter-productive to pass primitive types by reference? 7个
当我将诸如int和double之类的原语传递给函数时,最好是通过const引用或值传递它们(假设我不更改变量的值)?
int getValueFromArray(int index)
{
    // return the value from the array
}


int getValueFromArray(const int& index)
{
    // return the value from the array
}

谢谢

解决方法

对于基本类型,传递值比通过引用传递要好得多.不仅没有间接,而且通过引用,编译器必须担心潜在的混叠,这可能会破坏优化机会.

最后,pass-by-reference导致lvalues变得使用odr,这实际上可能导致链接器错误.如果电话被内联,这个最后的问题不会消失.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读