views:

493

answers:

2

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"?

+2  A: 

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.)

gabr
+1  A: 

Just to nitpick ;-).

The virtual destructor destroy already exists (and is used by Free). So if you declare a class like this you will get 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 else it can not be called.
  • final is used with virtual or override to define a method that can not be overridden.
  • reintroduce is used if you want to reintroduce a method that is already in a subclass without overriding it. It supresses the warning you get. And takes care of unwanted method hiding.
Gamecat