0

Ok. So I'm trying to figure out how to implement an overloaded operator. I've searched some tutorials but there's something I'm missing. Maybe it's one of those 'obvious' things I'm thinking too hard about. I don't know. Which is why I'm here.

Here's the overloaded operator:

public static bool operator +(Hero h, Monster m)

    {
        if (!h.IsRunningAway)
        {
            if(h.AttackSpeed > m.AttackSpeed)
            {
                m.takesDamage(h.attackValue());
                if (m.isAlive())
                {
                    h.takesDamage(m.AttackValue);
                }
            }
            else if(h.AttackSpeed < m.AttackSpeed)
            {
                h.takesDamage(m.AttackValue);
                if (h.isAlive())
                {
                    m.takesDamage(h.attackValue());
                }
            }
            else
            {
                h.takesDamage(m.AttackValue);
                m.takesDamage(h.attackValue());
            }
        }
        else
        {
            if(h.AttackSpeed <= m.AttackSpeed)
            {
                h.takesDamage(m.AttackValue);
            }              
        }
        h.IsRunningAway = false;
        return h.isAlive();
    }

Now, I'm trying to implement this in a button click event in a form.

 private void btnAttack_Click(object sender, RoutedEventArgs e)
    {

    }

Tutorials I've seen aren't being particularly clear on how to do it. Like I said, I maybe overthinking it. I usually do.

If the question can be edited to be clearer, or anything else is needed, let me know. I'm glad to edit.

2
  • Going to bed now. I'll check on comments in the morning and make whatever edit requests are asked of me then. Commented Apr 25, 2016 at 3:49
  • This is absolutely a horrible HORRIBLE abuse of operator overloading. This is what methods are to be used for. This will only create maintainability issues in your code. Commented Apr 25, 2016 at 3:52

1 Answer 1

1

DO NOT use operator overloading here!

Operator overloading should be used when it makes sense. For example, the - operator can be overloaded on DateTime. When you subtract one DateTime from another, it makes sense to get a time interval:

25/4/2016 - 24/4/2016 = 1 day

But in your case, you want to do addition on a Hero and a Monster, and you want to return a bool! That makes no sense at all! Can you tell me what does this mean?

Superman + Godzilla = True

Even if you can, don't do it. Because others don't know unless you need to explain your code to them. This is a maintenance nightmare!

What you should do instead, is to write a method in the Hero class.

Just from reading your code, I think you want the hero to kill the monster when you use the + operator. And return whether if the hero is alive after the fight. So this method will probably do the job:

///<summary>
///blah blah blah
///</summary>
///<returns>whether the hero is alive</returns>
public bool KillMonster(Monster m) {
    if (!this.IsRunningAway)
    {
        if(this.AttackSpeed > m.AttackSpeed)
        {
            m.takesDamage(this.attackValue());
            if (m.isAlive())
            {
                this.takesDamage(m.AttackValue);
            }
        }
        else if(this.AttackSpeed < m.AttackSpeed)
        {
            this.takesDamage(m.AttackValue);
            if (this.isAlive())
            {
                m.takesDamage(this.attackValue());
            }
        }
        else
        {
            this.takesDamage(m.AttackValue);
            m.takesDamage(this.attackValue());
        }
    }
    else
    {
        if(this.AttackSpeed <= m.AttackSpeed)
        {
            this.takesDamage(m.AttackValue);
        }              
    }
    this.IsRunningAway = false;
    return this.isAlive();
}

If you want to go one step further, you don't even need to return a value! Because the client code can call isAlive() to check whether he is alive, right? If isAlive() is private, make it public.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. That really is helpful. I was wondering about why an Operator Overload was wanted on this, too. But, that's what was required, so I've been trying to figure it out. Your method makes a bit more sense, though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.