operator overloading

hi folks,
today i meet a new problem again... =)
about operator overloading, as i know:
1
2
3
4
5
6
student operator+(student a){
 this.marks = this.marks + a.marks;
 return *this;
}

studentB = studentB + studentA

this is a simple operator overloading..

now i have another code:
1
2
3
4
5
6
student operator+(int a,student b){
 b.marks = a + b.marks;
 return b;
}

studentB = 5 + studentA

now these 2 code have the different usage rite?
first one is student+student, second one is int+student...

now the third code:
1
2
3
4
5
6
student operator+(student a, student b){
 a.marks = a.marks + b.marks;
 return a;
}

studentB = studentB + studentA

now this code also student + student, then whats the different between 1st code with 3rd code? if in my main function i have code:
studentA = studentA + studentB
which operator function will be called? First one or third one?

Thanks for reading...
1 needs to be a member of student, and you should be taking all your parameters by const reference. Also, your this. is incorrect, it should be this->.

As for your question I would say the first one would be called...but that's just me.
Last edited on
My guess is that studentA = studentA + studentB will generate a compilation error. At least this one does:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

ostream & operator<<(ostream & os, int n)
{
    return os;
}

int main()
{
    cout << 5 << endl;

    cin.get();
    return 0;
}

Because cout << 5 can be interpreted both as cout.operator<<(5) and as operator<<(cout,5)
Last edited on
Your first operator changes the semantics of the + operator.
The + operator should not modify either operand. (That is what the += operator is for.)

When dealing with a class, you often have the choice of specifying member operators or non-member operators. Both are equivalent when taking operands of the same type.

member
1
2
3
4
5
6
7
8
9
struct point
  {
  double x;
  double y;
  point(): x( 0.0 ), y( 0.0 ) { }
  point( double x, double y ) x( x ), y( y ) { }

  point operator + ( const point& rhs ) const;
  };


non-member
1
2
3
4
5
6
7
8
9
struct point
  {
  double x;
  double y;
  point(): x( 0.0 ), y( 0.0 ) { }
  point( double x, double y ) x( x ), y( y ) { }
  };

point operator + ( const point& lhs, const point& rhs );


If you want to mix types, say, "pt1 = 4 + pt2", or somesuch, then only the kind where the rhs operand is a different type may be member operators. The lhs operand operators must be non-member functions.
1
2
3
4
5
6
7
8
9
10
struct point
  {
  ...
  point operator + ( double rhs ) const;           // handles things like: pt + 5.3 
  };

point operator + ( double lhs, const point& rhs )  // handles things like: 5.3 + pt 
  {
  return rhs + lhs;
  }
In the case of commutative functions like +, you can simply define the non-member function in terms of the member function, as I did in the example.

Hope this helps.
oh..the reply giving lots of info thanks ...
but i have a question about Duoas reply..
i dun really understand about member and non-member function..
if i have a class point how my non-member function retrieve the data?
For example i using the same class as ur class point just change it to class not struct:
1
2
3
4
5
point operator+(double lhs, const point rhs){
       rhs.x = rhs.x+lhs;
       rhs.y = rhs.y+lhs;
       return rhs;
}


then how my rhs get y and x? since x and y is private.
Take a look at friend functions here -> http://cplusplus.com/doc/tutorial/inheritance/

In Duoas' example this isn't necessary as he made point a struct, which means that all members are public by default.
ok understood!
Also, remember that rhs is const, so your (point) + operator should look like:
1
2
3
4
5
6
point operator+(double lhs, const point rhs){
       point result;
       result.x = rhs.x+lhs;
       result.y = rhs.y+lhs;
       return result;
}
:-)
Topic archived. No new replies allowed.