19

I have following class:-

class myclass
{
    size_t st;

    myclass(size_t pst)
    {
        st=pst;
    }

    operator int()
    {
        return (int)st;
    }

    int operator+(int intojb)
    {
        return int(st) + intobj; 
    }

};

this works fine as long as I use it like this:-

char* src="This is test string";
int i= myclass(strlen(src)) + 100;

but I am unable to do this:-

int i= 100+ myclass(strlen(src));

Any idea, how can I achieve this??

nitin_cherian
  • 6,405
  • 21
  • 76
  • 127
Azher Iqbal
  • 547
  • 2
  • 4
  • 13

4 Answers4

27

Implement the operator overloading outside of the class:

class Num
{
public:
    Num(int i)
    {
        this->i = i;
    }

    int i;
};

int operator+(int i, const Num& n)
{
    return i + n.i;
}
Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636
  • 1
    +1. You should prefer the non-member versions anyway, even in cases where it's not necessary. Only use the member variants when you have to. – jalf Jul 27 '09 at 15:40
  • 1
    I always prefer to befriend my non-member operators. – Ben Voigt Apr 08 '10 at 03:26
13

You have to implement the operator as a non-member function to allow a primitive int on the left hand side.

int operator+( int lhs, const myclass& rhs ) {
    return lhs + (int)rhs;
}
Jeff L
  • 6,108
  • 3
  • 23
  • 29
5

The other answers here will solve the problem, but the following is the pattern I use when I'm doing this:

class Num
{
public:
  Num(int i)       // Not explicit, allows implicit conversion to Num
  : i_ (i)
  {
  }

  Num (Num const & rhs)
  : i_ (rhs.i_)
  {
  }

  Num & operator+= (Num const & rhs)  // Implement +=
  {
    i_ += rhs.i_;
    return *this;
  }

private:
    int i_;
};

//
// Because of Num(int), any number on the LHS or RHS will implicitly
// convert to Num - so no need to have lots of overloads
Num operator+(Num const & lhs, Num const & rhs)
{
  //
  // Implement '+' using '+='
  Num tmp (lhs);
  tmp+=rhs;
  return tmp;
}

One of the key benefits of this approach is that your functions can be implemented in terms of each other reducing the amount of overall code you need.

UPDATE:

To keep performance concerns at bay, I would probably define the non member operator+ as an inline function something like:

inline Num operator+(Num lhs, Num const & rhs)
{
  lhs+=rhs;
  return lhs;
}

The member operations are also inline (as they're declared in the class body) and so in all the code should be very close to the cost of adding two raw int objects.

Finally, as pointed out by jalf, the consequences of allowing implicit conversions in general needs to be considered. The above example assumes that it's sensible to convert from an integral type to a 'Num'.

Richard Corden
  • 21,389
  • 8
  • 58
  • 85
  • But there's no guarantee that converting from int is a meaningful operation. And the implicit conversation may be inefficient compared to just defining `operator+(int, Num)` – jalf Jul 27 '09 at 15:42
  • @jalf: Caveat for the conversion added. Regarding the implicit conversion, if the functions are inline then a good compiler should produce identical code for the above as it does for the (int, Num) case. – Richard Corden Jul 27 '09 at 16:40
  • Thanks for your answer, my question is: in the operator+= you give that the input is an object of type Num, but how about if I want to add an integer? (my g++ says I need to put an object as input) – Tomer Apr 16 '18 at 09:48
  • @Tomer: What example are you testing with? – Richard Corden Apr 17 '18 at 16:32
2

You need a global function operator+( int, myclass ) to do this:

int operator+( int intobj, myclass myobj )
{ return intobj + int(myobj); }
Peter Kovacs
  • 2,657
  • 21
  • 19