Shall I use “virtual” keyword for destructor of base-class which is actually an interface?

I have an abstract base class and 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 base-class destructor virtual, as in C++, or it`ll be OK if it is not virtual? By the way, did I use "override" right or I need "overload"?

Answers


Override is correct - you are redefining a virtual method.

If you really want TInterfaceMethod's destructor to throw EAbstractError, you'll have to mark it as 'override; abstract;'. (I'm surprised that it works, but I tested with D2007 and it does.) But why would you want to do that?

BTW, there is no need to use separate 'type' block for each declaration. You can format the code as that:

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;

Plus you should most probably use interfaces instead of an abstract base class. And you should mark TInterfaceMethod class 'abstract' as above. In theory, that would prevent you to create TInterfaceMethod object directly. (In practice, my D2007 allows that - weird.)


Need Your Help

Overloading Swift round function results in “Could not find an overload for 'init' that accepts the supplied arguments” error

ios swift method-overloading

I am new to Swift, and have just been playing around with some tutorials. I decided to try modifying the results of one of them by using the round function, but found that you couldn't specify 2 d...

find out the exact entity causing an exception in entity framework

entity-framework

The entity framework gives me generic messages in the exception without telling me the exact entity and the attribute which caused the error. How do I get more information about the error?