Overloading Various Other Operators: (), , -, , -, etc.

 < Day Day Up > 



Overloading Various Other Operators: (), +, -, <<, ->, etc.

This section will give you a hint as to how to overload various other operators that were not covered in previous sections. In your programming career you will be hard pressed to come up with a good reason to overload most of these operators.

The trusty Foo class will once again be used as a test class. Example 14.25 gives the function declaration with all the various operators declared. I will talk a little about each one.

Listing 14.25: foo.h

start example
  1  #ifndef _FOO_H  2  #define _FOO_H  3  4  class Foo{  5   public:  6       Foo(int _i = 0);  7       void setI(int _i);  8       int getI();  9       Foo& operator=(Foo& rhs); 10       Foo& operator+=(Foo& rhs); 11       Foo& operator<<(int shift_by);         //left shift 12       Foo& operator&(unsigned mask_value);   //bitwise and 13       Foo& operator()(int val1, int val2);   //function 14       Foo& operator+();                      //unary positive 15       Foo& operator-();                      //unary negate 16       Foo* operator->();                     //member of 17       Foo& operator,(Foo& rhs);              //comma 18       Foo& operator,(int);                   //comma 19   private: 20       int i; 21  }; 22 23   Foo& operator+(Foo& lhs, Foo& rhs); 24 25  #endif
end example

Referring to example 14.25, lines 9 and 10 declare the assignment and compound addition/assignment operators which you have seen before. The comments on lines 12 through 28 indicate the other operators being overloaded.

Example 14.26 gives the foo.cpp file with the implementations of these functions.

Listing 14.26: foo.cpp

start example
  1  #include "foo.h"  2  #include <iostream>  3  using namespace std;  4  5  Foo::Foo(int _i):i(_i){}  6  7  void Foo::setI(int _i){ i = _i;}  8  9  int Foo::getI(){return i;} 10 11  Foo& Foo::operator=(Foo& rhs){ 12     i = rhs.i; 13     return *this; 14  } 15 16  Foo& Foo::operator+=(Foo& rhs){ 17       i = i+rhs.i; 18       return *this; 19  } 20 21  Foo& Foo::operator<<(int shift_by){ 22       i <<= shift_by; 23       return *this; 24  } 25 26  Foo& Foo::operator&(unsigned mask_value){ 27       i &= mask_value; 28       return *this; 29  } 30 31  Foo& Foo::operator()(int val1, int val2){ 32       i += (val1 + val2); 33       return *this; 34  } 35 36  Foo& Foo::operator+(){ 37       i = (+i); 38       return *this; 39  } 40 41  Foo& Foo::operator-(){ 42       i = (-i); 43       return *this; 44  } 45 46  Foo* Foo::operator->(){ 47       return this; 48  } 49 50  Foo& Foo::operator,(Foo& rhs){ 51       cout<<"Foo::operator, followed by Foo object"<<endl; 52       rhs; //eliminates compiler warning - rhs parameter not used 53       return *this; 54  } 55 56  Foo& Foo::operator,(int){ 57       cout<<"Foo::operator, followed by int object"<<endl; 58       return *this; 59  } 60 61  /****************************************   62       Global additive operator   63   ****************************************/   64 65   Foo& operator+(Foo& lhs, Foo& rhs){   66       lhs.setI(lhs.getI() + rhs.getI());   67       return lhs;   68  }
end example

Example 14.27 gives a main() function showing these operators in action. Figure 14-11 shows the results of running Example 14.27

Listing 14.27: main.cpp

start example
  1  #include <iostream>  2  using namespace std;  3  #include "foo.h"  4  5  int main(){  6  7      Foo f1(3), f2(4);  8  9      f1+=f2; 10       cout<<f1.getI()<<endl; 11 12       f1<<5; 13 14        cout<<f1.getI()<<endl; 15 16       f1&0xfab5; 17 18       cout<<f1.getI()<<endl; 19 20       f1 = f1(3, 4); 21 22        cout<<f1.getI()<<endl; 23 24        f1 = f1 + f2; 25         cout<<f1.getI()<<endl; 26 27        f1 = (+f1); 28 29         cout<<f1.getI()<<endl; 30 31      f1 = (-f1); 32 33 34         cout<<f1.getI()<<endl; 35 36      int i = f1->getI(); 37 38      cout<<i<<endl; 39 40      f1,1,f2; 41 42      return 0; 43  }
end example

click to expand
Figure 14-11: Results of Running Example 14.27

Most of the operators in this section are implemented in a straightforward fashion. However, the operator()(), operator->(), and operator,() functions need some discussion.

The Function Operator: operator()()

The operator()() is the function operator. It is generally overloaded to do things the subscript operator cannot do. In this example, referring to the code shown in example 14.26 on lines 31, 32, and 33, the function operator is overloaded to take two integer parameters and add their value to the Foo object's i attribute. The function operator can take as many parameters as necessary and its implementation is limited only by your imagination.

The Member Operator: operator->()

The member operator implementation starts on line 46 of example 14.26. It is overloaded here to return a reference to the current Foo object. It could also be used to return references to other objects contained or pointed to by a Foo object.

The Comma Operator: operator,() - A.K.A. the Sequencing operator

Example 14.26 implements two versions of the comma operator. The version starting on line 50 implements the following Foo object version and the version starting on line 56 implements the following integer object version. The comma operator is very rarely overloaded!



 < Day Day Up > 



C++ for Artists. The Art, Philosophy, and Science of Object-Oriented Programming
C++ For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504028
EAN: 2147483647
Year: 2003
Pages: 340
Authors: Rick Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net