Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 1999-05

Calling a class method by pointer

5 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Shane Miller

Look in the TForms or TControls source. There are examples on this. I say....
if Assigned(FOnClick) then FOnClick(self);

FOnClick is set by saying FOnClick := @SomeProcedure
SomeProcedure is defined as
Procedure SomeProcedure(Sender : TObject);

Shane

Earlier message 72 lines
-----Original Message----- From: Michael A. Hess To: Lazarus-List Date: Sunday, May 16, 1999 1:32 PM Subject: [lazarus] Calling a class method by pointer >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? > >-- >==== Programming my first best destiny! ==== > >Michael A. Hess Miracle Concepts, Inc. > http://www.miraclec.com > >_________________________________________________________________ > To unsubscribe: mail with > "unsubscribe" as the Subject > archives at http://www.miraclec.com/list_archives/lazarus >

Reply by email

Michael A. Hess

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?

==== Programming my first best destiny! ==== Michael A. Hess Miracle Concepts, Inc. http://www.miraclec.com

Reply by email

Michael Van Canneyt
to Michael A. Hess
Michael A. Hess wrote 53 lines
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.

Reply by email

Michael A. Hess
to Shane Miller: Look in the TForms or TControls source. There are examples on this. I say.... if…
Shane Miller wrote 11 lines
Look in the TForms or TControls source. There are examples on this. I say.... if Assigned(FOnClick) then FOnClick(self); FOnClick is set by saying FOnClick := @SomeProcedure SomeProcedure is defined as Procedure SomeProcedure(Sender : TObject); Shane

I guess you didn't understand my email. It isn't that I don't know how to do it, I do. The problem is that the correct procedure is called but the application will abort with an Access Violation when it attempts to return from the procedure. If the procedure isn't part of a class in other words a class method it can be called and returns without any problem. In your examples you are calling a given class method from inside the class itself. You are just using a pointer of the same name and type.

Yes if you look at my example it to is trying to call it from inside the class but the way I want to implement it is by calling it from outside the class. I showed the example in the class to indicate that it doesn't work this way either. It gives the Access Violation. Also in my example I am casting a regular pointer to a new Type TProcedure where as yours was declared as the TNotifyEvent to begin with.

So why does that make it different? That is what I was asking! :-\

==== Programming my first best destiny! ==== Michael A. Hess Miracle Concepts, Inc. http://www.miraclec.com

Reply by email

Michael A. Hess
to Michael Van Canneyt: Definitely illegal; TMyClass.Stuff is a procedure OF OBJECT; So it should be type…
Michael Van Canneyt wrote 6 lines
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 ? :-)

Well obviously this is one area of Delphi I never played with much. :-)

I just never came across an occation where I needed to deal with passing procedure pointers outside of the class/object structure. :-)

However, thanks I get it know. :-)

==== Programming my first best destiny! ==== Michael A. Hess Miracle Concepts, Inc. http://www.miraclec.com

Reply by email