Difference between new and override?

前端 未结 2 703
青春惊慌失措
青春惊慌失措 2021-01-12 20:33

I have a base class which I want to provide some \'base\' functionality for methods for all inheriting classes.

In my inheriting classes I want to do:



        
相关标签:
2条回答
  • 2021-01-12 20:46

    It says so because you have not declared the base method virtual. Once you do so, the base virtual/derived override pair of keywords will make sense. Right now, the compiler is complaining that you cannot override a non-virtual method.

    0 讨论(0)
  • 2021-01-12 21:09

    When you use the override keyword the derived method is used even when the instance is passed as a Base class type. Hence there needs to be a virtual in the base class so that the programme knows it has to make a runtime check of what the actual type of the instance is. It then looks up what the actual type is and if it is a derived type it checks if their is an override in the derived class for that particular method in the base class.

    Member hiding is different. The base member will only be hidden if it is actually passed as an instance of the derived class. If the object is passed as a base class, the base class method will still be used. If you hide a member, you get a warning if you haven't use the new keyword. The new keyword is merely to tell the complier, that you really do mean to hide the base type method.

    0 讨论(0)
提交回复
热议问题