C++ overloaded operator with reverse order of associativity

You need a free function, defined after the class

struct A
{
   // ...
};

A operator+(int i, const A& a)
{
  return a+i; // assuming commutativity
};

also, you might consider defining A& operator+=(int i); in A an implement both versions of operator+ as free functions. You might also be interested in Boost.Operators or other helpers to simplify A, see my profile for two options.


Sure, define the inverse operator outside the class:

struct A
{
    int value;
    A operator+(int i) const
    {
        A a;
        a.value=value+i;
        return a;
    };
};
//marked inline to prevent a multiple definition
inline A operator+(int i, const A& a)
{
    return a + i;
}