DekGenius.com
[ Team LiB ] Previous Section Next Section

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:

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);
    }

    public 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 try and overload the +=, -=, /=, and *= operators directly, you can overload them indirectly by overloading the +, -, /, and * operators. The +=, -=, /=, and *= operators then 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 the integer constant will 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 *= 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 operator.

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 || f2 == null)
    {
        throw (new ArgumentException("Neither object may be null."));
    }

    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 Guideline," "Overloadable Operators," and "Operator Overloading Tutorial" topics in the MSDN documentation.

    [ Team LiB ] Previous Section Next Section