Delphi:构造不调用重写的虚拟构造函数

Ian*_*oyd 4 delphi virtual constructor delphi-5

我有一个例子的后代TBitmap:

TMyBitmap = class(TBitmap)
public
    constructor Create; override;
end;

constructor TMyBitmap.Create;
begin
   inherited;
   Beep;
end;
Run Code Online (Sandbox Code Playgroud)

在运行时,我构造其中一个TMyBitmap对象,将图像加载到其中,并将其放置TImage在窗体上:

procedure TForm1.Button1Click(Sender: TObject);
var
   g1: TGraphic;
begin
   g1 := TMyBitmap.Create;
   g1.LoadFromFile('C:\...\example.bmp');

   Image1.Picture.Graphic := g1;
end;
Run Code Online (Sandbox Code Playgroud)

在里面TPicture.SetGraphic你可以看到它通过构造一个新的,并调用.Assign新构建的克隆来制作图形的副本:

procedure TPicture.SetGraphic(Value: TGraphic);
var
   NewGraphic: TGraphic;
begin
   ...
   NewGraphic := TGraphicClass(Value.ClassType).Create;
   NewGraphic.Assign(Value);
   ...
end;
Run Code Online (Sandbox Code Playgroud)

构造新图形类的行:

NewGraphic := TGraphicClass(Value.ClassType).Create;
Run Code Online (Sandbox Code Playgroud)

正确调用我的构造函数,一切都很好.


我想做类似的事情,我想克隆一个TGraphic:

procedure TForm1.Button1Click(Sender: TObject);
var
   g1: TGraphic;
   g2: TGraphic;
begin
   g1 := TMyBitmap.Create;
   g1.LoadFromFile('C:\...\example.bmp');

   //Image1.Picture.Graphic := g1;
   g2 := TGraphicClass(g1.ClassType).Create;
end;
Run Code Online (Sandbox Code Playgroud)

除此之外从不调用我的构造函数,也不调用TBitmap构造函数.它只是调用TObject构造函数.施工后:

g2.ClassName: 'TMyBitmap'
g2.ClassType: TMyBitmap
Run Code Online (Sandbox Code Playgroud)

类型是正确的,但它不会调用我的构造函数,但其​​他地方的代码相同.

为什么?


即使在这个假设的假设的例子中,它仍然是一个问题,因为构造函数TBitmap没有被调用; 内部状态变量未初始化为有效值:

constructor TBitmap.Create;
begin
  inherited Create;
  FTransparentColor := clDefault;
  FImage := TBitmapImage.Create;
  FImage.Reference;
  if DDBsOnly then HandleType := bmDDB;
end;
Run Code Online (Sandbox Code Playgroud)

TPicture中的版本:

NewGraphic := TGraphicClass(Value.ClassType).Create;
Run Code Online (Sandbox Code Playgroud)

反编译为:

mov eax,[ebp-$08]
call TObject.ClassType
mov dl,$01
call dword ptr [eax+$0c]
mov [ebp-$0c],eax
Run Code Online (Sandbox Code Playgroud)

我的版本:

g2 := TGraphicClass(g1.ClassType).Create;
Run Code Online (Sandbox Code Playgroud)

反编译为:

mov eax,ebx
call TObject.ClassType
mov dl,$01
call TObject.Create
mov ebx,eax
Run Code Online (Sandbox Code Playgroud)

更新一

将"克隆"推送到单独的功能:

function CloneGraphic(Value: TGraphic): TGraphic;
var
    NewGraphic: TGraphic;
begin
   NewGraphic := TGraphicClass(Value.ClassType).Create;
   Result := NewGraphic;
end;
Run Code Online (Sandbox Code Playgroud)

没有帮助.

更新二

很明显,我清楚地提供了清晰的截图,清楚地显示了我的清晰代码,清楚地表明我清楚的代码显然是清楚的.很明显:

在此输入图像描述

更新三

这是一个带有OutputDebugStrings 的非官方版本:

{ TMyGraphic }

constructor TMyBitmap.Create;
begin
  inherited Create;
    OutputDebugStringA('Inside TMyBitmap.Create');
end;

function CloneGraphic(Value: TGraphic): TGraphic;
var
    NewGraphic: TGraphic;
begin
    NewGraphic := TGraphicClass(Value.ClassType).Create;
    Result := NewGraphic;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
    g1: TGraphic;
    g2: TGraphic;
begin
    OutputDebugString('Creating g1');
    g1 := TMyBitmap.Create;
    g1.LoadFromFile('C:\Archive\-=Images=-\ChessvDanCheckmateIn38.bmp');
    OutputDebugString(PChar('g1.ClassName: '+g1.ClassName));

    OutputDebugStringA('Assigning g1 to Image.Picture.Graphic');
    Image1.Picture.Graphic := g1;

    OutputDebugString('Creating g2');
    g2 := Graphics.TGraphicClass(g1.ClassType).Create;
    OutputDebugString(PChar('g2.ClassName: '+g2.ClassName));

    OutputDebugString(PChar('Cloning g1 into g2'));
    g2 := CloneGraphic(g1);
    OutputDebugString(PChar('g2.ClassName: '+g2.ClassName));
end;
Run Code Online (Sandbox Code Playgroud)

原始结果:

ODS: Creating g1 Process Project2.exe ($1138)
ODS: Inside TMyBitmap.Create Process Project2.exe ($1138)
ODS: g1.ClassName: TMyBitmap Process Project2.exe ($1138)
ODS: Assigning g1 to Image.Picture.Graphic Process Project2.exe ($1138)
ODS: Inside TMyBitmap.Create Process Project2.exe ($1138)
ODS: Creating g2 Process Project2.exe ($1138)
ODS: g2.ClassName: TMyBitmap Process Project2.exe ($1138)
ODS: Cloning g1 into g2 Process Project2.exe ($1138)
ODS: g2.ClassName: TMyBitmap Process Project2.exe ($1138)
ODS: g1.ClassName: TMyBitmap Process Project2.exe ($1138)
Run Code Online (Sandbox Code Playgroud)

格式化的结果:

Creating g1
   Inside TMyBitmap.Create
g1.ClassName: TMyBitmap

Assigning g1 to Image.Picture.Graphic
   Inside TMyBitmap.Create

Creating g2
g2.ClassName: TMyBitmap

Cloning g1 into g2
g2.ClassName: TMyBitmap

g1.ClassName: TMyBitmap
Run Code Online (Sandbox Code Playgroud)

更新四

我尝试关闭所有编译器选项:

在此输入图像描述

注意:请勿关闭Extended syntax.没有它你就不能分配Result一个函数(Undeclared identifier Result).

更新五

按照@David的建议,我尝试在其他一些机器上编译代码(所有Delphi 5):

  • Ian Boyd(我):失败(Windows 7 64位)
  • 戴尔:失败(Windows 7 64位)
  • 戴夫:失败了(Windows 7 64位)
  • 克里斯:失败(Windows 7 64位)
  • 杰米:失败(Windows 7 64位)
  • 周杰伦:失败(Windows XP 32位)
  • 客户构建服务器:失败(Windows 7 32位)

这是源头.

Dis*_*ned 7

这似乎是一个范围问题(以下是来自D5 Graphics.pas):

TGraphic = class(TPersistent)
...
protected
  constructor Create; virtual;
...
end;

TGraphicClass = class of TGraphic;
Run Code Online (Sandbox Code Playgroud)

您没有任何问题覆盖Create,并且TGraphicClass(Value.ClassType).Create;从Graphics.pas单元中调用时没有任何问题.

但是,在另一个单位TGraphicClass(Value.ClassType).Create;中无法访问受保护的成员TGraphic.因此,您最终调用TObject.Create;(非虚拟).

可能的解决方案

  • 编辑并重新编译Graphics.pas
  • 确保克隆方法子类在层次结构中较低.(例如TBitmap.Create是公开的)

编辑:附加解决方案

这是获取对类的受保护成员的访问权的技术的变体.
不保证解决方案的稳健性,但似乎确实有效.:)
你恐怕要进行自己的广泛测试.

type
  TGraphicCracker = class(TGraphic)
  end;

  TGraphicCrackerClass = class of TGraphicCracker;

procedure TForm1.Button1Click(Sender: TObject);
var
  a: TGraphic;
  b: TGraphic;
begin
  a := TMyBitmap.Create;
  b := TGraphicCrackerClass(a.ClassType).Create;
  b.Free;
  a.Free;
end;
Run Code Online (Sandbox Code Playgroud)