Is it not possible to call C++ operators manually?

后端 未结 6 602
执念已碎
执念已碎 2020-12-31 04:13

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

6条回答
  •  粉色の甜心
    2020-12-31 05:13

    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.

提交回复
热议问题