`obj.operator+=(rhs)` vs. `obj += rhs`

Viewed 91
class test
{
public:
    int i = 0;
    test& operator+=(const test &rhs)
    {
        i += rhs.i;
        return *this;
    }
};

int main()
{ 
    test t;
    test rhs;
    rhs.i = 10;
    // what's the difference betwen these 2?
    t.operator+=(rhs);
    t += rhs;
}

Is there any difference between t.operator+=(rhs); and t += rhs; here? I've always used the latter, and have never thought much about the former. Is there any advantage to using the former over the latter?

1 Answers

In most cases, there is no difference. When you write:

t += rhs

the compiler will process it as:

t.operator+=(rhs)

So, the two calls as just different syntaxes for the same call.

Related