19

I have overloaded the + operator like this

class sample
{
private : 
  int x;
public :
  sample(int x1 =0)
  {
    x = x1;
  }

  sample operator+(sample s);
};

sample sample::operator+(sample s)
{
  x = x + s.x;
  return *this;
}

int  main()
{
  sample s1(10);
  sample s2;
  s2 = s2 + s1;
  return 0;    
}

Is this correct? My question is If I want to add two different sample objects how will I overloaded the opeartor; e.g for s = s1 + s2;

I feel like doing s = s + s1 + s2 with the existing implementation.

Raulp
  • 7,758
  • 20
  • 93
  • 155
  • 2
    Look here! http://courses.cms.caltech.edu/cs11/material/cpp/donnie/cpp-ops.html or here http://www.cprogramming.com/tutorial/operator_overloading.html – trumpetlicks Jun 22 '12 at 17:57
  • 2
    note that `operator+` should _not_ change the value of the object on the left of the `+`, but yours does. If you wrote `s3 = s1 + s2`, your code would change `s1` to also hold the result. – Mooing Duck Jun 22 '12 at 18:02
  • Also, `main` has a `int` return type. – Mooing Duck Jun 22 '12 at 18:02
  • 2
    Also, you seem to be attempting to make the `operator+` a member function of the `sample` class, but you've done that completely wrong, this code [doesn't even come close to compiling](http://ideone.com/H7HjK). See http://sscce.org for how to write code for a question. – Mooing Duck Jun 22 '12 at 18:03

1 Answers1

31

Using friend operator overload should do the trick for you and is a common way to define binary operators, just add:

friend sample operator+(const sample& a, const sample& b); //in class

sample operator+(const sample& a, const sample& b) { //outside the class
    return sample(a.x + b.x);
}

If you want it to remain a member, (which has downsides in some rare scenarios, and no upsides), you have to make the operator a const function:

sample operator+(sample s) const; //in class

sample sample::operator+(const sample& b) const { //outside the class
    return sample(this->x + b.x);
}

Either of these will allow operator chaining. The reason your previous s = s + s1 + s2 was failing, is that the s + s1 would execute and return a temporary sample object. Then, it would attempt to add s2 to that sample. However, temporaries can only be const references[1], and as such, can only use const member functions. Since your operator+ member function is not a const function, you cannot use that function on the const temporary. Note that to make it const, I had to rewrite it, since your version modifies the object on the left side of the +.

[1]with exceptions aren't particularly relevant here, namely rvalues

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
higuaro
  • 15,730
  • 4
  • 36
  • 43
  • it looks Ok.Is it the only way to perfrom this using the friend.? – Raulp Jun 22 '12 at 18:00
  • Sorry, I don't understand the question, are you asking if using the `friend` keyword is the only way to achieve the requirement? – higuaro Jun 22 '12 at 18:18
  • i meant without using friend function can this be done.But I think this is the only way. – Raulp Jun 22 '12 at 18:20
  • @softy: no, you can do it without `friend` functions just as easily, but your code is wrong, and this is the "normal" way to do it – Mooing Duck Jun 22 '12 at 18:23
  • Yes you can do it without friend, but to do that you have to add a getter method for the `x` member variable – higuaro Jun 22 '12 at 18:23
  • @h3nr1x: ...or you simply make it a member function properly. (In this case, it needs to be a `const` function) – Mooing Duck Jun 22 '12 at 18:23
  • @softy: It's not really "different" enough from h3nr1x's answer, so I simply added it to his, with a full description of why. – Mooing Duck Jun 22 '12 at 18:57