> how do i know which Book (b1 or b2) is referred by the "this" keyword in the overloaded function ?
b2 + b1 is equivalent to either b2.operator+(b1) or operator+( b2, b1 )
> how to overload the plus operator ?
This is the canonical way:
1 2 3 4 5 6 7 8 9 10 11 12 13
struct A
{
// step one: first overload the compound assignment operator +=
A& operator+= ( const A& that ) { this->a += that.a ; return *this ; }
A& operator+= ( int n ) { this->a += n ; return *this ; }
int a = 0 ;
};
// step two: then overload the + operator in terms of the += operator
A operator+ ( A first, const A& second ) { return first += second ; }
A operator+ ( A first, int n ) { return first += n ; }
A operator+ ( int n, A first ) { return first += n ; }
If operator+ is a member function, b2 + b1 is equivalent to b2.operator+(b1)
The member function is invoked on object b2 and the this pointer points to b2
(the object on the left hand side of the +)