Passing an object by reference to an Overloaded operator - C ++

Brand new to C ++. I've seen people usually pass objects by reference when an operator is overloaded. Well, I can't figure out when it's really needed. As in the code below, if I remove the ampersand in the object declaration of c1 and c2 in operator +, I still get the same result. Is there any reason for passing by reference in this case where we don't want to change c1 or c2?

#include <iostream>

class Keys
    int m_nKeys;

    Keys(int nKeys) { m_nKeys = nKeys; }

    friend Keys operator+(const Keys &c1, const Keys &c2);

    int GetKeys() { return m_nKeys; }

Keys operator+(const Keys &c1, const Keys &c2)
    return Keys(c1.m_nKeys + c2.m_nKeys);

int main()
    Keys cKeys1(6);
    Keys cKeys2(8);
    Keys cKeysSum = cKeys1 + cKeys2;
    std::cout << "There are " << cKeysSum.GetKeys() << " Keys." << std::endl;
    return 0;



source to share

4 answers

Operators are like regular functions, just with "fancy" names :)
(for example, operator+()

instead of sum()


So, the same rules for passing parameters that you apply to functions can also apply to overloaded operations.

Specifically, when you have a parameter that is not cheap to copy (for example, int

a float

are examples of cheap to copy parameters; a std::vector

, a std::string

are examples of not cheap to copy parameters) and you observe that parameter inside your method (i.e. e. it is an input parameter is read-only ), then you can pass it by const reference . (const &)

So basically it's the same as the address of the original argument being passed to the function, so no deep copy involved . Deep copies can be very expensive, for example. think of a vector with a lot of elements.

So, to repeat, you pass a const reference when:

  • the parameter is just not cheap to copy (e.g. for ints, floats, etc. don't worry: passing by value is just fine)
  • the parameter is observed in the implementation of the function / operator (i.e. it is a read-only input parameter)


If you pass by reference, then there is no copy of the object, which for more complex classes can significantly improve performance.

In this case, the execution cost may be negligible, and it is entirely possible that the compiler could optimize the whole thing, but it is still worth doing. Later, the Keys class can evolve into something more complex.



Consider a vector

of long

with 10 million records. If you prototype a function like:

void foo(vector<long> vl)


This will result in an assignment-operator (or copy-constructor) vector<long>

- and it will need to copy all those 10m . A later destructor for this temporary object ( vl

) will release memory and perform other cleanup. This will definitely affect performance

There are classes especially related to synchronization providers (critical sections, etc.) and some smart pointer classes that prevent the creation of copy-copies and / or assignment operators, so creation or creation of objects does not happen in error. Although move-constructor or move-assign-operator could be implemented.



Benefits of following the link:

  • This allows us to change the function of the argument, which is sometimes useful.
  • Since no copy of the argument is executed, it is fast, even when used with large structures or classes.
  • We can follow the const reference to avoid unintentional changes.
  • We can return multiple values ​​from a function.

Disadvantages of following the link:

  • Since a non-constant reference cannot be made for a literal or expression, the reference arguments must be normal variables.
  • It is difficult to tell if a parameter passed by reference should be injected, displayed, or both.
  • It is impossible to tell from a function call that an argument can change. The argument passed by value and passed by reference looks the same. We can only indicate whether the argument is passed by value or by reference by looking at the function declaration. This can lead to situations when the programmer does not implement the function, changes the value of the argument.
  • Since references are usually implemented by C ++ using pointers, and dereferencing a pointer is slower than accessing it directly, accessing values ​​passed by reference is slower than accessing values ​​passed by value.

You can read below:



All Articles