Can I get a link to the result of the "operator"?

Somehow it is possible to get a reference to the result of an overloaded operator in C #, so you don't have to use a "new" keyword to create a temporary result (which is returned after that)?

Here is an example of the problem I am facing:

public class Stats {

    public float someField;
    public float someOtherField;

    public static Stats operator +(Stats a, Stats b) {
        Stats c = new Stats(); // I don't want a new one, can I access operators result directly?
        c.someField = a.someField + b.someField;
        c.someOtherField = a.someOtherField + b.someOtherField;
        return c;
    }

    /*
    // This is what I want to achieve, but it would be cooler if static and with the "+"
    public Add(SomeType a) {
        someField += a.someField;
        someOtherField += a.someOtherField
    }
    */
}

public class StatObserver {
    public Stats statsToObserve;

    public Output() {
        print(statsToObserve.someField);
    }
}

public class Class {
    public Stats firstStats = new Stats();
    firstStats.someField = 1.5f;

    public StatObserver showStats = new StatObserver();
    showStats.statsToObserve = firstStats;

    public Stats nextStats = new Stats();
    nextStats.someField = 3.4f;

    // now the tricky part
    firstStats += nextStats; // C# handles the += itself correctly

    showStats.Output(); // prints "1.5"

    // you have to update the observer to get the new value
    // it kind of stupid, because you have to treat firstStats like a value type buts its not
    showStats.statsToObserve = firstStats;
    showStats.Output(); // prints "4.9"
}

      

+3


source to share


2 answers


You cannot overload the operator directly +=

- it is compiled to add and assign. You can mutate the left side as part of the operator +

, but that would be evil. The method Add

seems to be the cleanest design IMHO.



+2


source


First of all, as @ D.Stanley points out, you cannot override +=

. You can override +

how you did it, but there is something important to understand +

:

Mathematical operators are non-destructive, i.e. return the result without changing the operands

That being said, you can change the properties of the operands in the reference type (what it is), but you shouldn't. Don't do that. The good news is that your operation is +

correct.

Because of this, you need to return an object new

(as you do), but when you do +=

, you assign a local reference to that new object, leaving the observer reference to the old object, throwing an error.

You probably want to change the observer directly:



showStats.statsToObserve += nextStats;

      

Or you can completely hack it and do this ( not recommended ):

public static Stats operator +(Stats a, Stats b) {
    Stats c = new Stats();
    c.someField = a.someField + b.someField;
    a.SomeField += b.someField; //AHHHH You just modified an operand!
    c.someOtherField = a.someOtherField + b.someOtherField;
    a.someOtherField += b.someOtherField; //AHHHH You just did it again!
    return c;
}

      

Note. I love @ DStanley's recommendation and also the solution to this problem.

+2


source







All Articles