JAN*_*JAN 6 c++ pass-by-reference pass-by-value
当我将原语int和double函数一起传递给函数时,最好是通过const引用或值传递它们(假设我不改变变量的值)?
int getValueFromArray(int index)
{
// return the value from the array
}
int getValueFromArray(const int& index)
{
// return the value from the array
}
Run Code Online (Sandbox Code Playgroud)
谢谢
对于基本类型,传递值比通过引用传递要好得多.不仅没有间接,而且通过引用,编译器必须担心潜在的混叠,这可能会破坏优化机会.
最后,pass-by-reference导致lvalues变得使用odr,这实际上可能导致链接器错误.如果电话被内联,这个最后的问题不会消失.
| 归档时间: |
|
| 查看次数: |
1704 次 |
| 最近记录: |