Remember, you cannot use a friend to overload the assignment operator. The assignment operator can be overloaded only by a member operator function.
Here operator+( ) is overloaded for the coord class by using a friend function:
//Overload the + relative to coord class using a friend.
#include
using namespace std;
class coord {
int x, y; // coordinate values
public:
coord( ) { x = 0; y = 0; }
coord(int i, int j) { x = i; y = j; }
void get_xy(int &i, int &j) { i = x; j = y; }
friend coord operator+(coord ob1, coord ob2);
};
// Overload + using a friend.
coord operator+(coord ob1, coord ob2) {
coord temp;
temp.x = ob1.x + ob2.x;
temp.y = ob1.y + ob2.y;
return temp;
}
int main( ) {
coord o1(10, 10), o2(5, 3), o3;
int x, y;
o3 = o1 + o2; //add to objects
// this calls operator+( )
o3.get_xy(x, y);
cout << "(o1+o2) X: " << x << ", Y: " << y << "\n";
return 0;
}
Note that the left operand is passed to the first parameter and the right operand is passed to the second parameter.
Overloading an operator by using a friend provides one very important feature that member functions do not. Using a friend operator function, you can allow objects to be used in operations involving build-in types in which the built-in type is on the left side of the operator:
ob1 = ob2 + 10; // legal
ob1 = 10 + ob2; // illegal
The solution is to make the overloaded operator functions, friend and define both possible situations.
As you know, a friend operator function is explicitly passed both operands. Thus, it is possible to define one overloaded friend function so that the left operand is an object and the right operand is the other type. Then you could overload the operator again with the left operand being the built-in type and the right operand being the object. For example,
// Use friend operator functions to add flexibility.
#include
using namespace std;
class coord {
int x, y; // coordinate values
public:
coord( ) { x = 0; y = 0; }
coord(int i, int j) { x = i; y = j; }
void get_xy(int &i, int &j) { i = x; j = y; }
friend coord operator+(coord ob1, int i);
friend coord operator+(int i, coord ob1);
};
// Overload for obj + int.
coord operator+(coord ob1, int i) {
coord temp;
temp.x = ob1.x + i;
temp.y = ob1.y + i;
return temp;
}
// Overload for int + obj.
coord operator+(int i, coord ob1) {
coord temp;
temp.x = ob1.x + i;
temp.y = ob1.y + i;
return temp;
}
int main( ) {
coord o1(10, 10);
int x, y;
o1 = o1 + 10; // object + integer
o1.get_xy(x, y);
cout << "(o1+10) X: " << x << ", Y: " << y << "\n";
o1 = 99 + o1; // integer + object
o1.get_xy(x, y);
cout << "(99+o1) X: " << x << ", Y: " << y << "\n";
return 0;
}
As a result of overloading friend operator functions both of these statements are now valid:
o1 = o1 + 10;
o1 = 99 + o1;
If you want to use friend operator function to overload either ++ or -- unary operator, you must pass the operand to the function as a reference parameter. This is because friend functions do not have this pointers. Remember that the increment or decrement operators imply that the operand will be modified. If you pass the operand to the friend as a reference parameter, changes that occur inside the friend function affect the object that generates the call. Here an example,
// Overload the ++ relative to coord class using a
// friend.
#include
using namespace std;
class coord {
int x, y; // coordinate values
public:
coord( ) { x = 0; y = 0; }
coord(int i, int j) { x = i; y = j; }
void get_xy(int &i, int &j) { i = x; j = y; }
friend coord operator++(coord &ob);
};
// Overload ++ using a friend.
coord operator++(coord &ob) {
ob.x++;
ob.y++;
return ob;
}
int main( ) {
coord o1(10, 10);
int x, y;
++o1; //o1 is passed by reference
o1.get_xy(x, y);
cout << "(++o1) X: " << x << ", Y: " << y << "\n";
return 0;
}
With modern compiler, you can also distinguish between the prefix and the postfix form of the increment or decrement operators when using a friend operator function in much the same way you did when using member functions. For example, here are the prototypes for both versions of the increment operator relative to coord class:
coord operator++(coord &ob); // prefix
coord operator++(coord &ob, int notused); // postfix
No comments:
Post a Comment