1

Is there a difference between

class MyString {
...
    MyString &operator=(MyString other) {
        swap(s, other.s);
        return (*this);
    }
};

and

class MyString {
...
    MyString &operator=(MyString other) {
        swap(s, other.s);
        return *this;
    }
};

I read in some other posts that adding parenthesis indicates that you are returning by reference rather than value. But in case the return type is defined, it doesn't seem to make a difference.

nhtrnm
  • 1,411
  • 3
  • 15
  • 24
  • In your case, the additional parenthesis don't make any difference. Use whichever coding style you like. – R Sahu Nov 11 '20 at 05:59

1 Answers1

4

The link you have cited discusses a different situation. In this case

*this == (*this)

It depends on your preference since both of them will do exactly the same thing. Personally, I'd prefer *this to avoid unnecessary parenthesis

Operator precedence can sometimes change the way you want to de-reference something. Imagine this situation

*my_vector[0]

What do you think will happen here? Is it doing

(*my_vector)[0]

Or

*(my_vector[0])

The correct answer is the second one. Sometimes you might want to change this behavior, and that's when () come.