I see that in a lot of examples in c++ that operator overloading gets as an argument a class instance passed by reference or if it returns a class instance it returns it by reference as well. Is there any reason why people choose to pass it by reference while passing a pointer / by value will work as well ? An example :
Class MyClass
{
public:
int m_num;
MyClass() { m_num = 1;}
const MyClass operator+(const MyClass& mcls)
{
MyClass temp;
temp = m_num + mcls.m_num;
return temp;
}
}
Assume that we overload the = operator.
So taking for example the code I wrote, sending arguments to the operator+ overload function NOT by reference AND ALSO returning an instance of MyClass not by reference will work as well yet I see in a lot of examples it's passed and returned by reference and I would like to know if there's a reason behind this or it's just convention of some type or maybe a preference ?