gpt4 book ai didi

c++ - 按值传递或按引用传递

转载 作者:太空狗 更新时间:2023-10-29 20:06:18 25 4
gpt4 key购买 nike

void Subroutine1(int Parameter1)

void Subroutine2(const int &Parameter1)

在 Subroutine1 中,我们必须获取参数的拷贝,而在 Subroutine2 中,我们不必制作拷贝,这可能会节省一些开销。

在实践中,Subroutine1 似乎比另一个更频繁地使用。为什么会这样?

最佳答案

In practice Subroutine1 seems being used more often than the other. Why is that the case?

因为复制一个 int 比创建一个引用(或指针)然后访问它要好。

更一般地说,所有原始类型都应该按值传递。

关于c++ - 按值传递或按引用传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8433824/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com