X1020 Constructing instance of '%s' containing abstract method '%s.%s' (Delphi)

From RAD Studio
Jump to: navigation, search

Go Up to Error and Warning Messages (Delphi)

The identifier for the $WARN directive: CONSTRUCTING_ABSTRACT

The code you are compiling is constructing instances of classes which contain abstract methods.

program Produce;
{$WARNINGS ON}
{$WARN CONSTRUCTING_ABSTRACT ON}

  type
    Base = class
      procedure Abstraction; virtual; abstract;
    end;

  var
    b : Base;

begin
  b := Base.Create;
end.

An abstract procedure does not exist, so it becomes dangerous to create instances of a class which contains abstract procedures. In this case, the creation of 'b' is the cause of the warning. Any invocation of 'Abstraction' through the instance of 'Base' created here would cause a runtime error.

One solution to this problem is to remove the abstract directive from the procedure declaration, as is shown in the following example:

program Solve;

  type
    Base = class
      procedure Abstraction; virtual;
    end;

  var
    b : Base;

  procedure Base.Abstraction;
  begin
  end;
 
begin
  b := Base.Create;
end.