通过引用c ++ [英] pass by reference c++

查看:182
本文介绍了通过引用c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的老师在c ++告诉我,调用引用应该只使用,如果我不会改变任何东西在数组里面的函数。
我有一些真正大的向量,我在我的程序传递。所有的向量将在函数内部修改。我的矩阵的大小约为 [256 * 256] [256] [50] ...



一些特殊原因不在这里使用调用引用?



AFAIK调用通过引用应该更快,消耗更少的内存?


当你不改变函数希望更改反映到原始数组,不用考虑要在原始数组中反映的更改。



它应该如果您不要希望您的函数更改您的原始数组(您需要在调用后保留原始值),并且被调用函数更改传递的参数的值。 p>

My teacher in c++ told me that call by reference should only be used if I'm not going to change anything on the arrays inside the function. I have some really big vectors that I'm passing around in my program. All the vectors will be modified inside the functions. My matrices are of sizes about [256*256][256][50]...

Is there some particular reason not to use call-by reference here?

AFAIK call by reference should be way faster and consume less memory?

解决方案

My teacher in c++ told me that call by reference should only be used if I'm not going to change anything on the arrays inside the function.

It should be used when you are not changing something inside the function or you change things and want the changes to be reflected to the original array or don't care about the changes to be reflected in the original array.

It shouldn't be used if you don't want your function to change your original array (you need to preserve the original values after the call) and the callee function changes the values of the passed argument.

这篇关于通过引用c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆