Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do I often see references in operator overloading definitions?

For example, in the OGRE3D engine, I often see things like

class_name class_name :: operator + (class_name & object)

Instead of

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 ?

like image 694
jokoon Avatar asked Jan 22 '23 05:01

jokoon


1 Answers

It's a performance issue. Passing by reference will generally be cheaper than passing by value (it's basically equivalent to passing by pointer).

On an unrelated note, you probably want the argument to operator+ to be const class_name &object.

like image 93
Oliver Charlesworth Avatar answered Feb 16 '23 02:02

Oliver Charlesworth