[fpc-pascal]Interfaces, What am I doing wrong?
memsom at interalpha.co.uk
memsom at interalpha.co.uk
Mon May 12 12:11:55 CEST 2003
I knocked up the following solution.
You do not need a GUID unless you want to use COM or a COM like interface.
Borland have actually altered Interfaces in D6 onwards so that IUnknown
inherits from IInterface and IInterface implements nothing at all (or very
little anyway) to help facilitate the practice of Interfaces outside of COM.
NB. didn't actually compile this so it could fail. I think the uses clause is
complete. Moved some of your fields/methods around for better encasulation.
Matt
////////////////////////////////////////////////////////////
unit QBase;
interface
{$MODE ObjFPC}
uses
SySutils, Classes;
type
IQObject = interface
procedure SetName(iName: string); //these are annoying, but are needed
function GetName: string;
// iirc you need the full definition
property Name: string read GetName write SetName;
end;
TQObject = class(TInterfacedObject, IQObject)
private
fName: string;
protected
//interfaces don't care about scope so long as it's at least protected
//because private scop is invisible to descendents and this will break
//the interface I guess (though I've never tried this out)
procedure SetName(iName: string); virtual;
function GetName: string; virtual;
public
constructor Create;
destructor Destroy; override;
property Name: string read GetName write SetName;
end;
//convenience routine
function NewQObjectIntf(AName: string = ''): IQObject;
implementation
function NewQObjectIntf(AName: string = ''): IQObject;
begin
Result := TQObject.Create;
if (AName <> '') then
Result.Name := AName;
end;
constructor tQOTQObjectbject.Create;
begin
fName := 'TQObject';
end;
destructor TQObject.Destroy;
begin
inherited; // << you NEEEEED this
end;
procedure TQObject.SetName(iName: string);
begin
Writeln('Renaming ', fName, ' to ', iName);
fName := iName;
end;
function TQObject.GetName: string;
begin
Result := fName;
end;
end.
---------------------------------------------
This message was sent using Mistral WebMail.
http://www.mistral.co.uk/
More information about the fpc-pascal
mailing list