is this not a correct way to overload binary plus operator as class member function

struct integer
{
int x;
void set(int i)
{
this->x=i;
return;
}
int get()
{
return this->x;
}
integer&operator+(const integer&v1,const integer &v2)
{
set(v1.get()+v2.get());
return (*this);
}

};
int main(void)
{
console::clear();
integer v1,v2,v3;
output::string("enter a number");
v1.set(input::integer());
v2.set(input::integer());
v3.operator+(v1,v2);
output::integer(v3.get());
output::string("=");
output::integer(v1.get());
output::string("+");
output::integer(v2.get());
console::pause();
return 0;
}

what is wrong in the given code and why?
As a member function this binary operator + is incorrect

integer&operator+(const integer&v1,const integer &v2)
{
set(v1.get()+v2.get());
return (*this);
}

In fact it has three parameters. Implicit pointer to the object itself and two explicitly declared parameters. The operator as a member function shall be declared the following way

1
2
3
4
5
6
7
8
const integer operator +( const integer &rhs ) const
{
   integer temp( *this );

   temp.set( temp.get() + rhs.get() );

   return ( temp );
}


And moreover function get shall have the const qualifier

1
2
3
4
int get() const
{
   return this->x;
}
Last edited on
No that is not correct.

Binary operators take 2 parameters, one for each side of the operator. If you define your operator as part of a class, then the first parameters is automatically considered to be this, which means your function would only take one parameter.

An example of this:

1
2
3
4
5
6
7
8
class MyClass
{
  MyClass operator + (const MyClass& rhs) const
  {
    // here, '*this' is the left hand side of the + operator
    //   and 'rhs' is the right hand side
  }
};


If the operator is defined globally, the 'this' pointer does not exist. Therefore both left and right sides must be passed to the function:

1
2
3
4
5
6
7
8
9
10
11
12
13
class MyClass
{
  // .. operator not in the class
};


//defined globally (outside the class)
MyClass operator + (const MyClass& lhs, const MyClass& rhs)
{
  // here, there is no '*this'  so instead...
  //  'lhs' is the left side
  //  and 'rhs' is the right side
}



Note that in each case, the object being assigned to the sum is not represented. That is... c = a + b;.... only a and b are represented. c is not part of the + operator at all.

Also note that I don't return a reference in my examples, I return a copy (value). This is because you do not want to modify 'this'.. but rather you want to create an entirely new object and return that.





Finally... this is nonsense:

v3.operator+(v1,v2);

The proper way to do it would be like this:

 
v3 = v1 + v2;


v1 would be either 'this' or 'lhs' depending on whether the operator is global or not. And v2 would be 'rhs'. The new object that is generated by the + operator is returned, then gets assigned to v3 by the assignment operator.
I'd like to append my previous message that to show how to call the operator. You can call it either as

v3 = v1.operator+( v2 );
or

v3 = v1 + v2;
Last edited on
dammit vlad, you ninja ;)
Question from another C++ newbie:

Could someone tell me what header(s) are missing on this file, to make it build? I added <iostream>, but input, output, console are all undefined...
This can say only the author of the code because he uses its own headers as I see. But all you need is the class definition. I think you will can to add input and output statements youself.
Last edited on
Ahhhh... I get it now...
Since vlad and Disch were both treating the OP's code as working code, I thought it was! (as in, it is complete and compiles as presented). It didn't occur to me that the original example actually was not complete!

Since all I really wanted to know about was the operator+ , I switched to cstdio for I/O, and now I see it working! Thanks!
@vald & disch thanx to all

I got it...
Topic archived. No new replies allowed.