I\'m trying to understand operators in C++ more carefully.
I know that operators in C++ are basically just functions. What I don\'t get is, what does the function lo
As has been mentioned in commentary and in other answers, by there is no operator+
for fundamental types. For classes, the answer to which of operator+(x,y)
versus x.operator+(y)
is correct is "it depends". Particularly, it depends on how operator+
was defined. If it was defined as an member function then you need to use x.operator+(y)
. If it was defined as a global function then you need to use operator+(x,y)
.
When the compiler confronts the statement z=x+y;
your compiler is smart enough to look for the appropriate form. You shouldn't be expecting one or the other. You should be using x+y
.