Jump to content
  • Advertisement
Sign in to follow this  
Daniel Miller

[.net] c#: overloading operator=

This topic is 4770 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

Considering that you can't overload operator=, how are you supposed to allow something like this? MyClass obj = new MyClass(); obj = "hello"; edit: Besides, of course, a normal method. ;)

Share this post


Link to post
Share on other sites
Advertisement
Now that I think about it, an implicit cast overload won't work... because the MyClass that would get returned would completely overwrite the reference that you assign it to rather than use any other operator.

I don't think you're going to be able to do it using operator =

I think the closest thing you'll be able to come to is a property that accepts a string, and give it some easy to use name.

obj.Iterator = "hello";


public string Iterator
{
set
{
// do whatever kind of iterator "find" code you want in order to set the internal iterator member.
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Daniel Miller
Actually, I want to allign an iterator-ish thing with the matching object in a collection.

That seems really nonintuitive for operator=. What if you *did* want the iterator to point at a different collection? I agree with the behavior of not overloading operator=. Stick with something like iterator.MoveTo("myObject"), iterator["myObject"], etc.

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!