Recipe3.7.Indirectly Overloading the , -, , and Operators


Recipe 3.7. Indirectly Overloading the +=, -=, /=, and *= Operators

Problem

You need to control the handling of the +=, -=, /=, and *= operators within your data type; unfortunately, these operators cannot be directly overloaded.

Solution

Overload these operators indirectly by overloading the +, -, /, and * operators, as demonstrated in Example 3-9.

Example 3-9. Overloading the +, -, /, and * operators

 public class Foo  {     // Other class members…     // Overloaded binary operators     public static Foo operator +(Foo f1, Foo f2)     {        Foo result = new Foo( );        // Add f1 and f2 here…        // place result of the addition into the result variable.        return (result);     }     public static Foo operator +(int constant, Foo f1)     {        Foo result = new Foo( );        // Add the constant integer and f1 here…        // place result of the addition into the result variable.        return (result);     }     public static Foo operator +(Foo f1, int constant)     {        Foo result = new Foo( );        // Add the constant integer and f1 here…        // place result of the addition into the result variable.        return (result);     }     public static Foo operator -(Foo f1, Foo f2)     {        Foo result = new Foo( );        // Subtract f1 and f2 here…        // place result of the subtraction into the result variable.        return (result);     }     public static Foo operator -(int constant, Foo f1)      {        Foo result = new Foo( );        // Subtract the constant integer and f1 here…        // place result of the subtraction into the result variable.        return (result);      }     public static Foo operator -(Foo f1, int constant)      {        Foo result = new Foo( );        // Subtract the constant integer and f1 here…        // place result of the subtraction into the result variable.        return (result);      }      public static Foo operator *(Foo f1, Foo f2)      {        Foo result = new Foo( );        // Multiply f1 and f2 here…        // place result of the multiplication into the result variable.        return (result);     }     ypublic static Foo operator *(int multiplier, Foo f1)     {        Foo result = new Foo( );        // Multiply multiplier and f1 here…        // place result of the multiplication into the result variable.        return (result);     }     public static Foo operator *(Foo f1, int multiplier)     {        return (multiplier * f1);     }     public static Foo operator /(Foo f1, Foo f2)     {        Foo result = new Foo( );        // Divide f1 and f2 here…        // place result of the division into the result variable.        return (result);     }     public static Foo operator /(int numerator, Foo f1)     {        Foo result = new Foo( );        // Divide numerator and f1 here…        // place result of the division into the result variable.        return (result);     }     public static Foo operator /(Foo f1, int denominator)     {         return (1 / (denominator / f1));      } } 

Discussion

While it is illegal to overload the +=, -=, /=, and *= operators directly, you can overload them indirectly by overloading the +, -, /, and * operators. The +=, -=, /=, and *= operators use the overloaded +, -, /, and * operators for their calculations.

The four operators +, -, /, and * are overloaded by the methods in the Solution section of this recipe. You might notice that each operator is overloaded three times. This is intentional, since a user of your object may attempt to add, subtract, multiply, or divide it by an integer value. The unknown here is: which position will the integer constant be in? Will it be in the first parameter or the second? The following code snippet shows how this might look for multiplication:

 Foo x = new Foo( ); Foo y = new Foo( ); y *= 100;      // Uses: operator *(Foo f1, int multiplier)  y = 100 * x;    // Uses: operator *(int multiplier, Foo f1) y *= x;       // Uses: operator *(Foo f1, Foo f2) 

The same holds true for the other overloaded operators.

If these operators were being implemented in a class, you would first check whether any were set to null. The following code for the overloaded addition operator has been modified to do this:

 public static Foo operator +(Foo f1, Foo f2) {    if (f1 == null)    {       throw (new ArgumentNullException("f1"));    }    else if (f2 == null)    {       throw (new ArgumentNullException("f2"));    }    else    {       Foo result = new Foo( );       // Add f1 and f2 here…       // place result of the addition into the result variable.       return (result);    } } 

See Also

See the "Operator Overloading Usage Guidelines," "Overloadable Operators," and "Operator Overloading Tutorial" topics in the MSDN documentation.



C# Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2004
Pages: 424

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