How to inherit generic virtual method?

后端 未结 1 776
遇见更好的自我
遇见更好的自我 2021-01-12 09:52

I have the following code. I want to override the Notify method of the base base list on this to be able to create an event on the modification of the list.

         


        
相关标签:
1条回答
  • 2021-01-12 10:33

    If your descendant class is fixing the generic type then you have to use that fixed type in place of T. In your case :

    protected
      procedure Notify(const Value: TSomeclass;
                       Action: TCollectionNotification); override;
    

    is the correct way to declare this function.


    The error :

    Declaration of 'Notify' differs from previous declaration

    is a regrettable case of the Delphi RTL duplicating type names in different units.

    The unit System.Classes defines

    TCollectionNotification = (cnAdded, cnExtracting, cnDeleting);
    

    and System.Generics.Collections defines

    TCollectionNotification = (cnAdded, cnRemoved, cnExtracted);
    

    Almost certainly you have Generics.Collections declared before Classes in your uses clause and the compiler is resolving the undesired version of TCollectionNotification.

    To fix it, either reorganize your uses clauses so that Generics.Collections comes after Classes or use a fully qualified type name, ie :

      procedure Notify(const Value: TSomeClass;
        Action: Generics.Collections.TCollectionNotification); override;
    

    The lesson with a differs from previous declaration error is to methodically check your types. Ctrl+CLICK on the type identifier will take you to the definition of the type the compiler is using.

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