Here is the code:
#include <iostream> using namespace std; class X { public: int x; X(int x=6) : x{x} {} void print() {cout << x;} X operator+(int rop) const { return rop + x; } int operator+(const X& rop)const { return x+rop.x; } }; X operator+(int lop, const X& x) { return 2*lop + x.x; } int main() { X x; cout << (5+x) + (x+2); x.print(); return 0; } Here we see different overloading operators, that overloads addition. In my example for ( 5+x) was called 2*lop + x.x; and for (x+2) rop + x;(I suppose)
But I cannot really understand why(especially in the first case) for (5+x) 2*lop + x.x; was called? And in general can you explain the differences between these overloading operators?
operator+for the case where int + X - the order of operands is important+should be commutative. Mixing member and non-member+is somewhat bad, but having return types such thatx + x + x + 1uses 3 different functions? disgusting