Damned books.

Published March 14, 2007
Advertisement
Guess you learn something every day...

One of the major gripes I hear about C# is being unable to properly overload operators for things. The assignment operator in particular is touchy since that would be the common way to allow your class/struct to interpret other types.

Recently too I've run across this in my attempts to create a class which acted as a view/subset of others depending on permissions.

So I tried something peculiar today, which happens to work, despite being fairly non-intuitive... a search of course yields the first link as MSDN using conversion operators which does what I tried, a conversion operator set in the class which converts the random other object into the specified class (rather than the class into random types, which is the example given in every book and every tutorial I've seen up until today).

And of course it works great. user-class = builtin-class behaves as expected. I imagine there might be problems for slightly more complex classes, and the conversion + copy-constructor probably adds an unnecessary copy but I also imagine things that require operator= overloads aren't too big or complex...

Still I'm curious as to why such a construct isn't suggested more in the threads bemoaning the lack of an operator= overload. I'm also thrilled since this will likely allow my view mechanism to finally work.
Previous Entry Moe plans.
Next Entry Moe work delayed.
0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement