私はいくつかのパッケージを含むプロジェクトに取り組んでいます。私の基本パッケージの一つで、私は(ここでは完全なコードがある)そのようなスマートポインタを宣言:Delphi - スマートポインタコンストラクタでの不思議な動作
unit UTWSmartPointer;
interface
type
IWSmartPointer<T> = reference to function: T;
TWSmartPointer<T: class, constructor> = class(TInterfacedObject, IWSmartPointer<T>)
private
m_pInstance: T;
public
constructor Create; overload; virtual;
constructor Create(pInstance: T); overload; virtual;
destructor Destroy; override;
function Invoke: T; virtual;
end;
implementation
//---------------------------------------------------------------------------
constructor TWSmartPointer<T>.Create;
begin
inherited Create;
m_pInstance := T.Create;
end;
//---------------------------------------------------------------------------
constructor TWSmartPointer<T>.Create(pInstance: T);
begin
inherited Create;
m_pInstance := pInstance;
end;
//---------------------------------------------------------------------------
destructor TWSmartPointer<T>.Destroy;
begin
m_pInstance.Free;
m_pInstance := nil;
inherited Destroy;
end;
//---------------------------------------------------------------------------
function TWSmartPointer<T>.Invoke: T;
begin
Result := m_pInstance;
end;
//---------------------------------------------------------------------------
end.
その後、私のプロジェクトで(別のパッケージに)、私は(GDI +オブジェクトと、このスマートポインタを使用しますTGpGraphicsPath)。私は、コードを実行するとき
...
pGraphicsPath: IWSmartPointer<TGpGraphicsPath>;
...
pGraphicsPath := TWSmartPointer<TGpGraphicsPath>.Create();
...
しかし、何も画面上に描画されていない:私はそのようなグラフィックのパスを宣言します。私はエラーも例外もアクセス違反もなく、空白のページしか得られません。しかし、私はちょうどそのような私のコードを変更した場合:
...
pGraphicsPath: IWSmartPointer<TGpGraphicsPath>;
...
pGraphicsPath := TWSmartPointer<TGpGraphicsPath>.Create(TGpGraphicsPath.Create);
...
は、すべての罰金となり、そして予想通り、私のパスが正確に描かれています。しかし、私は最初のコンストラクタが期待通りに動作しない理由を理解できません。誰かが私にこの奇妙な行動を説明することができますか?
よろしくお願いいたします。
をTGpGraphicsPath.Create(optionalArgument) - ジェネリックで注意する必要があるもの。 – Dsm
@Remy匿名メソッドは実際にはインターフェイスであり、その実装の詳細はこのように広く使用されていることが判明しました:http://stackoverflow.com/a/39955320 –
@Dsm 'TGpGraphicsPath'はコンストラクタにパラメータを持ちません。 –