简体   繁体   English

对象未创建

[英]Object doesn't creating

I'm creating a non-visual component. 我正在创建一个非可视组件。 Short Code: 短代码:

// Element of some list item
TMyItem = class
private
  Id: integer;
  Caption: string;
public
  constructor Create(const aId: integer; const aCaption: string);
end;

// List of items
TMyItemList = class(TObjectList<TMyItem>)
public
  constructor Create;
end;

// The component
TMyComp = class(TComponent)
private
  FMyList: TMyItemList;
public
  constructor Create;

implementation

constructor TMyComp.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FMyList:= TMyItemList.Create;
end;

Question: why "FMyList" not exists when "MyComp" created? 问题:为什么在创建“ MyComp”时不存在“ FMyList”? Assigned(FMyList) = false ... 分配的(FMyList)= false ...

This is not your actual code. 这不是您的实际代码。 Your actual code compiles. 您的实际代码会编译。 But we can guess what the problem is. 但是我们可以猜测出问题所在。 Here: 这里:

type
  TMyComp = class(TComponent)
  private
    FMyList: TMyItemList;
  public
    constructor Create;
  end;

The constructor for TComponent is declared like this: TComponent的构造函数声明如下:

constructor Create(AOwner: TComponent); virtual;

It's a virtual constructor because the Delphi streaming framework relies upon metaclasses to create objects. 这是一个虚拟的构造函数,因为Delphi流框架依赖于元类来创建对象。 If you want the framework to call your constructor you have to override this virtual constructor. 如果希望框架调用构造函数,则必须重写此虚拟构造函数。

constructor Create(AOwner: TComponent); override;

I'm reasonably confident of this guess, but we should not need to guess. 我对此猜测相当有信心,但我们不必猜测。 Next time, please submit real code, a Minimal, Complete, and Verifiable Example . 下次,请提交真实代码,一个最小,完整且可验证的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM