-2

I have the following problem:

I can statically overload the operator+ to add some custom objects:

public static obj operator+(obj A, obj B)
{
    obj ret = new obj();
    ret.magic = A.magic + B.magic;
    return ret;
}

The return is a new object that got whatever summed by operator+. Which is perfect.

But when writing…

obj A, B;
/* ... */
A += B;

This would not increment A, it would construct a new A. So A does not only hold a new value, it is a completely new object, which is not what I want when I write A += B.

So what is the C# approach to overloading unary assignment operators like I do in C++?

Because, just leaving out the first line in above static operator+ - function will change A in the following scenario:

obj C = A + B;

so, for pretty obvious reasons:

after A += B, A should still be the same object.

Of course, I can just implement it the way it should be:

public obj add(obj B)
{
    magic += B.magic;
    return this;
}

public static obj operator+(obj A, obj B)
{
    obj ret = new obj();
    ret.magic = A.magic;
    ret.add(B);
    return ret;
}

But then, it still wont get called on A += B, which will break expectations.

rhavin
  • 1,512
  • 1
  • 12
  • 33
  • Possible duplicate of [Simple way to overload compound assignment operator in C#?](http://stackoverflow.com/questions/2869245/simple-way-to-overload-compound-assignment-operator-in-c) – finlaybob Mar 27 '17 at 08:15
  • The operator overloading code of yours creates a new object and sums up the values of a and b to is magic property. You should do `a.magin += b.magin; return a;` to see the changes happening in object a. – Chetan Mar 27 '17 at 08:16
  • Possible duplicate of [Overloading assignment operator in C#](http://stackoverflow.com/questions/4537803/overloading-assignment-operator-in-c-sharp) – Nuri YILMAZ Mar 27 '17 at 08:18

3 Answers3

0

Try

public static obj operator+(obj a, obj b)
{
    a.magic += b.magic;
    return a;
}
stelioslogothetis
  • 9,371
  • 3
  • 28
  • 53
  • that would change a in obj c = a + b; – rhavin Mar 27 '17 at 08:16
  • This code worked perfectly. It modified the property of obj a with the summed up value. Did you try this code @rhavin ? Did it not give the proper value ? – Chetan Mar 27 '17 at 08:23
0

a += b; is a shortcut to a = a + b

So reusing the same object or creating new one is whole responsibility of + operator.

Technically, all "standard" implementations of + always create new object, but with structs you just don't notice that.

You can implement your + logic in a way that it just returns left operand.

Lanorkin
  • 7,310
  • 2
  • 42
  • 60
  • Well, in c++ it is not. there it is a 'modify a by b' not a 'create new anonymous, modify it by a and b and reassign to a' - which is clearly not what i need. – rhavin Mar 27 '17 at 08:29
  • @rhavin This is C# question. – Lanorkin Mar 27 '17 at 08:36
  • Yeah, thats why i'm asking how to do it in C#. – rhavin Mar 27 '17 at 08:37
  • @rhavin - try to understand the answers you already have. I here and @Sty below shows you how to implement `+` in such a way it uses the provided object instead of creating new one - that's just *your* code in `+` operator. – Lanorkin Mar 27 '17 at 08:39
  • Please try to understand the problem, not providing a solution for something different. – rhavin Mar 27 '17 at 08:42
-1

You are not able to directly overload "+=" operator, you can read more at MSDN

Basically, behavior depends on your "+" operator overload. (You are creating new object in that method)

Darjan Bogdan
  • 3,780
  • 1
  • 22
  • 31
  • Down for stating something I already know (its in the title!) and not adressing the problem at all. – rhavin Mar 27 '17 at 09:03
  • @rhavin well problem is obvious, I stated it inside braces, you mustn't create new object if you want `A` to be the same object. – Darjan Bogdan Mar 27 '17 at 09:17