Should I use the "virtual" keyword for the base class destructor, which is actually an interface?

I have an abstract base class and a derived class:

type TInterfaceMethod = class
  public
    destructor Destroy; virtual; abstract;
    procedure Calculate; virtual; abstract;
    procedure PrepareForWork; virtual; abstract;
end;
type ConcreteMethod = class(TInterfaceMethod)
  private
    matrix: TMinMatrix;
  public
    constructor Create(var matr: TMinMatrix);
    procedure Calculate; override;
    procedure PrepareForWork; override;
    destructor Destroy;
end;

      

Do I really need to make the base class destructor virtual like in C ++, or will it be ok if it's not virtual?
BTW, did I use the "override" right or do I need an "overload"?

0


source to share


2 answers


The override is correct - you are overriding a virtual method.

If you really want the TInterfaceMethod destructor to throw EAbstractError, you will need to mark it as "override; Abstract; '. (I'm surprised it works, but I tested it with D2007 and it does.) But why do you want this? to do?

By the way, there is no need to use a separate type block for each declaration. You can format your code like this:



type
  TInterfaceMethod = class abstract
  public
    destructor Destroy; override; abstract;
    procedure Calculate; virtual; abstract;
    procedure PrepareForWork; virtual; abstract;
  end;

  TConcreteMethod = class(TInterfaceMethod)
  private
    matrix: TMinMatrix;
  public
    constructor Create(var matr: TMinMatrix);
    procedure Calculate; override;
    procedure PrepareForWork; override;
    destructor Destroy; override;
  end;

      

Also, you should most likely use interfaces instead of an abstract base class. And you should note the thesis of the TInterfaceMethod class as above. In theory, this could prevent you from directly creating a TInterfaceMethod object. (In practice, my D2007 allows this - strange.)

+3


source


Simple for the nitpick; -).

The virtual destructor destroy already exists (and is being used by Free). Therefore, if you declare such a class, you will have some problems.



But to summarize some method directives:

  • virtual is used to define a virtual method (uses runtime binding).
  • override is used to define a method that overrides a virtual method.
  • abstract is used with virtual (or override!) to define a method without implementation. It must be overridden in a subclass, otherwise it cannot be called.
  • final is used with virtual or override to define a method that cannot be overridden.
  • reintroduce is used when you want to reintroduce a method that is already in the subclass without overriding it. It suppresses the warning you receive. And takes care of hiding the unwanted method.
+1


source







All Articles