Suppose I have this class:
class Int{
private:
int x;
public:
Int(int i) : x(i) {}
};
Can I build a prefix + operator to write expression such x = + a b?
I tried to overload operator+ inside the class but the compiler complain that operator+ must be unary or binary and not ternary.
Is there a way to do such thing?
You can’t change the way that the ‘+’ character is interpreted by the compiler – it has special syntactic significance. If you’re willing to give up the + symbol, though, you could create a class called
plusand overload operator() to take its following arguments.Essentially, you’re creating a dsl using the
plusclass as a proxy for the + operator.