Method overloads resolution and Jon Skeet's Brain Teasers

前端 未结 5 1541
無奈伤痛
無奈伤痛 2020-12-13 06:45

Jon\'s Brain Teasers

Here Be Spoilers...

I\'m looking at the answer to #1, and I must admit I never knew this was the case in overload resol

5条回答
  •  青春惊慌失措
    2020-12-13 07:38

    Here is a possible explanation:

    When the compiler links the method calls, the first place it looks in in the class that is lowest in the inheritance chain (in this case the Derived class). It's instance methods are checked and matched. The overridden method Foo is not an instance method of Derived, it is an instance method of the Base class.

    The reason why could be performance, as Jack30lena proposed, but it could also be how the compiler interprets the coder's intention. It's a safe assumption that the developer's intended code behavior lies in the code at the bottom of the inheritance chain.

提交回复
热议问题