[lazarus] Calling a class method by pointer
Michael Van Canneyt
michael.vancanneyt at wisa.be
Mon May 17 04:22:55 EDT 1999
On Sun, 16 May 1999, Michael A. Hess wrote:
> Greetings,
>
> Should I be able to call a class method by passing a pointer to that
> method and then running the method by referencing the pointer.
>
> The following is a crude layout and not meant to be compiled and tested.
>
> type
> TMyClass = class(some parent)
> .
> .
> procedure Stuff;
> procedure Setup;
> end;
>
> TProcedure = procedure;
>
>
> procedure PlainProcedure;
> begin
> ... does stuff
> end;
>
> procedure TMyClass.Stuff;
> begin
> ... does stuff
> end;
>
> procedure TMyClass.Setup;
> var
> proc : Pointer;
> begin
> proc := @PlainProcedure;
>
> // now try and call the plain procedure
>
> TProcedure(proc);
>
> // This works, does stuff, and returns correctly.
>
> proc := @Stuff;
>
> // now try and call the class procedure
>
> TProcedure(proc);
>
> // it called Stuff and did whatever was in it but gives an
> // access violation when it attempts to return.
>
> end;
>
> Should the above type of scenerio, calling a class method by pointer, be
> able to work or am I trying to do something that is normally illegal?
Definitely illegal; TMyClass.Stuff is a procedure OF OBJECT;
So it should be
type
TMyClass = class(some parent)
.
.
procedure Stuff;
procedure Setup;
end;
TProcedure = procedure;
TObjProcedure = Procedure of object;
procedure PlainProcedure;
begin
... does stuff
end;
procedure TMyClass.Stuff;
begin
... does stuff
end;
procedure TMyClass.Setup;
var
proc : Pointer;
stuffproc : TObjProcedure;
begin
proc := @PlainProcedure;
TProcedure(proc);
Stuffproc := Self.stuff;
StuffProc; <<-- Procedure of object, NOT simply procedure.
end;
---------------------------------------------------------
With a 'procedure of object' a pointer to an instance also must be passed.
This is not the case with a simple procedure.
I thought you would have known this ? As a experienced Delphi programmer ?
:-)
Michael.
More information about the Lazarus
mailing list