[C#] One set of overloaded operators transferred to many classes. via /r/learnprogramming


[C#] One set of overloaded operators transferred to many classes.

I'm trying to code something for a game and I'd love a better alternative.

I've created a vector class that overloads most of the math operators so that they do component addition/subtraction/multiplication etc. It's array based, and I use an enum to give it names. That way altering a specific vector is really easy. I can just remove or add things to the enum.

I do this a lot. Things like resist, stats, attributes can all be summed up really fast inside my characters class and makes everything easier to code and modify as I go. Originally I was going to inherit from the vector class, but children won't inherit my operators. The only solution I found online was to use overloaded methods. At that point, I'd be writing a lot of duplicate code.

I feel like I'm missing some very obvious solution. I'd love some ideas or ways to do this that are a bit cleaner and are not tedious.

Submitted July 16, 2017 at 02:00AM by throwayprog
via reddit http://ift.tt/2tfbw1X

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s