2

In C++ the first operand(object) will call the overloading function. What will do the same in c#? which operand? Because in C# we have both operands sent to the function for example for + like this:

public static Rectangle operator + (Rectangle x,Rectangle y)

And the second question. If we want to perform this for two different types (consider object1 and object2 from different class types) where should we define the function?

user2808671
  • 271
  • 2
  • 13
  • Your understanding of C++ would benefit from reading http://stackoverflow.com/a/4421729/103167 – Ben Voigt Sep 23 '13 at 20:50
  • If you have two questions **please post two questions**. Posting two questions in the same question is confusing. – Eric Lippert Sep 23 '13 at 21:39
  • @Eric: There's not really two questions here, just bad wording. The question is "If a binary operator has operands of two different types, will both classes be searched for a user-defined operator definition?" He seems to think (wrongly) that in C++ only the type of the left operand is searched, and wants to know if C# requires that. – Ben Voigt Sep 23 '13 at 21:42

3 Answers3

3

In your example, the overloaded operator is static (indeed, all overloaded operators are in C#), so neither operand is the "one" calling the method, unlike the instance method object.Equals, where in x.Equals(y) you could point out x as being the one you're calling the method on. Instead, it's just like you made this static method call:

Rectangle.Add(x, y);

If you have two different types, you can define the operator in either class, but not in both classes.

Tim S.
  • 55,448
  • 7
  • 96
  • 122
  • You mean in case of having two different types if we define it in one class the compiler itself looks at the parameters and finds an appropriate function in one of the two classes? – user2808671 Sep 23 '13 at 20:51
  • Thanks. Think I Completely got how it works. So if we have similar/different parameters or any other situation, the function will be called exactly like a static function of a class. I think the compiler finds the the function according to the parameters (looks for the definitions in their classes) – user2808671 Sep 23 '13 at 21:05
3

In C++ the first operand(object) will call the overloading function. What will do the same in c#?

This is confusing but I think I understand what you're asking. Are you saying that in C++, when you say

a + b

you really get

a.operator+(b)

? That is, the left hand operand is the receiver of the call in C++, is it the same in C#?

No. That's not how C# works. Operators are always static; they have no receiver. Both operands are passed as arguments.

And the second question.

If you have two questions in the future please post two questions. Posting two questions in the same question is confusing.

If we want to perform this for two different types (consider object1 and object2 from different class types) where should we define the function?

It must be in one of the two classes but not both. Which one you pick is up to you.

Incidentally, I'll be discussing the differences between C++ and C# overloading of the ++ operator on my blog this Wednesday, so if this topic interests you, check it out.

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
  • I think that's what he's saying, but C++ supports operator dispatch on both arguments as well (statically), with just a few exceptions that are required to be member functions (and C# doesn't allow user definitions for those operators at all). – Ben Voigt Sep 23 '13 at 21:39
0

As I understand C#'s operand structure, neither operand calls the operator. The operator is a function which in your case takes the rectangle arguments x and y

Anthony
  • 286
  • 3
  • 11