1

lets say I got a class A and I want to overload an operator, when should I use

A& operator+(const A& b)
  {
    A c;
    c.val = this->val + b.val
    return c;
  }

or

A operator+(const A& b)
  {
    A c;
    c.val = this->val + b.val
    return c;
  }

without the '&' thing?

tbsnsa
  • 29
  • 6
  • 5
    If you are returning a reference to itself `return *this;` you'll want the return type to be `A&`. If you are returning a local variable or local parameter variable, you'll want to return by value. – Eljay Aug 11 '21 at 16:22
  • Using the result of your first version would be undefined behaviour, as you have returned a reference to an object that ceases to exist. – Caleth Aug 11 '21 at 16:24
  • Handy reading: [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading) – user4581301 Aug 11 '21 at 16:25
  • Important clarification: how well do you understand what the `&` represents? – user4581301 Aug 11 '21 at 16:27
  • When you're returning a reference, ask yourself "a reference to what?" In the case of `operator+`, your first version returns a reference to a local variable that immediately goes out of scope, invalidating that reference. That's no good. But sometimes "a reference to the calling object" or "a reference to an element in a container" or the like is a helpful thing to return. – Nathan Pierson Aug 11 '21 at 16:28
  • There's nothing special about operator overloads; they just have funky names (and a "magical" transformation from operator syntax to function call). The same principles about not returning dangling references apply as anywhere else. – molbdnilo Aug 11 '21 at 16:30
  • @user4581301 i think i dont understand it very well, sorry – tbsnsa Aug 11 '21 at 16:36
  • 2
    @tbsnsa Then you're learing the language in a confusing order. You should learn what references are before attempting to understand operator overloading. This should be of use: https://stackoverflow.com/q/388242/2079303 – eerorika Aug 11 '21 at 16:38
  • Nothing to be sorry about. Just take eerorika's advice otherwise you probably will be sorry. – user4581301 Aug 11 '21 at 16:46
  • @user4581301 yeah, i actually came up with this question when i wonder what is the "std::ostream& os" takes in "std::ostream& operator<<(std::ostream& os, const T& obj)" and thats where i started to confused about this & thing, is cout are some kind of lvalue? – tbsnsa Aug 11 '21 at 17:03

1 Answers1

1

when should i use '&' when overloading an operator?

You should use & on a type when ever you want that type to be a reference.

Typically, a reasonable rule of thumb is to do what the corresponding built-in operators do. If the corresponding built-in operator is a prvalue, then return an object in your overload, and if the built-in operator is an lvalue, then return an lvalue reference.

For example, 1+2 is a prvalue expression, so following the described advice, you should return an object when overloading the binary operator+.

A& operator+(const A& b)
  {
    A c;
    c.val = this->val + b.val
    return c;
  }

This function returns a dangling reference. If the caller attempts to access the referred value, then the behaviour of the program will be undefined. Don't return a reference (nor pointer) to an automatic variable.

eerorika
  • 232,697
  • 12
  • 197
  • 326