• Advertisement
  • entries
    359
  • comments
    237
  • views
    188666

Damned books.

Sign in to follow this  

52 views

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.
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement