0

I have this code, the parent class has an abstract bool Filter(EntityComponent entity) but in this class, I prefer implementing bool Filter(BaseComponent entity) because others are calling the latter as well (EntityComponent is a BaseComponent). To prevent StackOverflow, I cast it to BaseComponent so the correct overload can be called:

protected override bool Filter(EntityComponent entity) => Filter((BaseComponent)entity);

protected bool Filter(BaseComponent entity)
{
    return
        (AllowBeavers && entity.GetComponentFast<BeaverSpec>())
        || (AllowBots && entity.GetComponentFast<BotSpec>());
}

Now I am surprised that VS asks me to remove the cast, and it even refers to the correct method when removing such cast:

enter image description here

Why is Filter(BaseComponent) being chosen when EntityComponent surely is more detailed? How would that not make a recursion without the cast?

2
  • I find the setup confusing, can you show minimal reproducible example please? Did I replicate the case correctly? I find confusing that abstract class knows about ihnerited concrete class. Commented Mar 7 at 12:53
  • 1
    see this answer Commented Mar 7 at 12:59

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.