Skip to main content
gamedev.net
🔒 Locked

operator overloading....

Started by Ancient Spirit 4 replies 1.1k views
Original Post
Ancient Spirit
Ancient Spirit
Hello, can you please tell me why do the overloaded operators in C# has to be static? Thanks, Spirit
This world is ruled by big furry CATS!! :)Meow! :)
MaulingMonkey
MaulingMonkey
Quote:
Original post by Ancient Spirit
Hello, can you please tell me why do the overloaded operators in C# has to be static?


Sounds like the creators of C# had a bout of sanity. Or you could label them stupid/lazy, but then I'd disagree with you. A correct answer no matter the case would be "because that's the way it was designed". Unless this isn't really a requirement, I've only touched C#, not really used it.

Is there an issue you're having with this requirement which we may be able to explain to you how to work around?
Ancient Spirit
Ancient Spirit
I come from a world of C++ and im used to override operators just by writing the regular thing... But the thing is that i just dont understand why it has to be static, i mean static is a way to call methods without insitiating the class right? why do we need it then?

And i actually like the idea of .NET....
This world is ruled by big furry CATS!! :)Meow! :)
Fruny
Fruny
Ok, I'm basing my reasoning on C++. If the (static) operator's definition require you to explicitely specify both left and right parameters, that's because it is necessary if you want the left parameter to not be of the type of the class. e.g. 1+x. Non-static members, relying on an implicit first parameter can only have left parameters that are of the type of the class: x+1. That's why, in C++, it is generally necessary to define binary operators as non-member functions.

I figure the C# designers just got rid of the possibility of defining operators either as non-static members or non-members (or rather, static, given how C# is designed), and just left static operators.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
MaulingMonkey
MaulingMonkey
Quote:
Original post by Ancient Spirit
I come from a world of C++ and im used to override operators just by writing the regular thing...


I come from a world of C++, and "the regular thing" for me is static operators. They're more generic/versitile, and allow for the class to be the right hand side as well. C#'s creators probably just came from the same world of C++ that I did.
Ancient Spirit
Ancient Spirit
Thanks, i think i got it :)
This world is ruled by big furry CATS!! :)Meow! :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.