Singleton
Ifølge http://community.borland.com/article/0,1410,22576,00.htmlskulle denne implementation af en singleton virke.
Men jeg får en fejl i denne linje:
Instance := inherited NewInstance;
[Error] SingletonUnit.pas(36): Incompatible types: 'TSingleton' and 'TObject'
unit SingletonUnit;
interface
type
TSingleton = class
public
class function NewInstance: TObject; override;
procedure FreeInstance; override;
class function RefCount: Integer;
end;
implementation
{ TSingleton }
var
Instance : TSingleton = nil;
Ref_Count : Integer = 0;
procedure TSingleton.FreeInstance;
begin
Dec( Ref_Count );
if ( Ref_Count = 0 ) then
begin
Instance := nil;
// Destroy private variables here
inherited FreeInstance;
end;
end;
class function TSingleton.NewInstance: TObject;
begin
if ( not Assigned( Instance ) ) then
begin
Instance := inherited NewInstance;
// Initialize private variables here, like this:
// TSingleton(Result).Variable := Value;
end;
Result := Instance
Inc( Ref_Count );
end;
class function TSingleton.RefCount: Integer;
begin
Result := Ref_Count;
end;
end.
jeg fårstår ikke fejlen da TSingleton jo er af typen 'TObject'.
Nogen som kan hjælpe?
