Jump to content
  • Advertisement
Sign in to follow this  
Ancient Spirit

operator overloading....

This topic is 4656 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
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?

Share this post


Link to post
Share on other sites
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....

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!