jeudi 28 avril 2016

Operator overloading C++ reference or value

I've seen many tutorials and tried to find the answer on stackoverflow but with no success. What I'm not sure is, is there some praxis when to return by value or by reference, when overloading operator?

For ex.

Class &operator+(){
  Class obj;
  ...
  return obj;
}

or the same thing but by value

Class operator+(){
  Class obj;
  ...
  return obj;
}

And yeah I'd like to mention, I've noticed that in almost 90% of cases when returning the same object(*this), is being reference on the same object returned. Could someone explain why is that so, as well? Thanks!

Aucun commentaire:

Enregistrer un commentaire