여러 패키지가 포함 된 프로젝트에서 작업하고 있습니다. 그 (여기가 전체 코드입니다)처럼 내 기본 패키지 중 하나에서 나는, 스마트 포인터를 선언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이 TObject.Create가 대신 호출되는 경우 선택적 인수를 취한다는 내 생각 엔 것 TGpGraphicsPath.Create (optionalArgument) - 제네릭을주의해야합니다. – Dsm
@Remy 익명 메소드가 실제로 인터페이스라는 사실이 밝혀졌습니다.이 구현 방법은 다음과 같이 널리 사용됩니다. http://stackoverflow.com/a/39955320 –
@Dsm'TGpGraphicsPath'에는 생성자에 매개 변수가 없습니다. . –