我经常在运算符重载定义中看到哪些引用? [英] What do I often see references in operator overloading definitions?
问题描述
例如,在OGRE3D引擎中,我经常看到类似的东西
For example, in the OGRE3D engine, I often see things like
class_name class_name :: operator + (class_name & object)
代替
class_name class_name :: operator + (class_name object)
不是我更喜欢第二种形式,但是在输入中使用引用是否有特定的原因?是否有特殊情况需要使用引用而不是按值复制?还是性能问题?
Well it's not that I prefer the second form, but is there a particular reason to use a reference in the input ? Does it has special cases where it is necessary to use a reference instead of a copy-by-value ? Or is a performance matter ?
推荐答案
这是性能问题.通常,按引用传递要比按值传递便宜(它基本上等效于按指针传递).
It's a performance issue. Passing by reference will generally be cheaper than passing by value (it's basically equivalent to passing by pointer).
在不相关的注释上,您可能希望operator+
的参数为const class_name &object
.
On an unrelated note, you probably want the argument to operator+
to be const class_name &object
.
这篇关于我经常在运算符重载定义中看到哪些引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!