I usually use GetImplementor

IMyInterface = interface(IInterface)
 [...]
 function GetImplementor: TObject;
 ...
end;

TMyObject = class(TInterfacedObject, IMyInterface)
...
protected
 function GetImplementor: TObject;
 ...
end;

function TMyObject.GetImplementor: TObject;
begin
 Result:= Self;
end;


[]s

Cesar Romero
http://blogs.liws.com.br/cesar


Albert Zeyer escreveu:
Hi,

Is there a way to cast an interface to an object?

If I have an interface:

IBlub = interface
  function GetSomething(): TObject;
end;

And somewhere, I have:

var
  Blub: IBlub;
...

Therefore, Blub is an interface-reference to some object which implements the interface. How can I get this TObject? A simply cast like TObject(Blub) surely does not work, because Blub points to the function table of the interface. But Blub itself has to save the object somewhere because if I now call Blub.GetSomething(), I have Self inside of GetSomething.

I could make the following implementation for GetSomething:

function TSomeBlub.GetSomething(): TObject;
begin
  Result := Self;
end;

(I would rename it to GetSelf or GetImplementingObject or perhaps GetImplementingClass (then I would return Self.ClassType).)

But is there a better, direct way? It seems like a hack/workaround to implement such a function.

Albert


_________________________________________________________________
    To unsubscribe: mail [EMAIL PROTECTED] with
               "unsubscribe" as the Subject
  archives at http://www.lazarus.freepascal.org/mailarchives

Reply via email to