Why isn't compatible procedure and method pointer?

HeloI'm writing an simulation of console, but in graphical mode. It uses objects. I must pass address of procedure which handles window messages. But I can't pass procedure in object. Can someone help?Thanks

Comments

: Helo: I'm writing an simulation of console, but in graphical mode. It uses objects. I must pass address of procedure which handles window messages. But I can't pass procedure in object. Can someone help?: Thanks: :

I think I understand what you are trying to point out.Passing "@obj.procedure" wouldn't work, because internally object procedures are called like "procedure(objectptr, params,...)", so there is only one procedure in memory ( <-> not one procedure for every instance).

What you could do in this case is to pass the object (the instance - some people get confused with "class" and "object/instance" and ObjPas does it's best to "support" this confusion) and then call your "handle" procedure on that object instead of trying to pass the object's handle procedure itself.

However, this would mean that you should use an interface (or something similiar in ObjPas). Like:

: : Helo: : I'm writing an simulation of console, but in graphical mode. It uses objects. I must pass address of procedure which handles window messages. But I can't pass procedure in object. Can someone help?: : Thanks: : : : : : I think I understand what you are trying to point out.: Passing "@obj.procedure" wouldn't work, because internally object procedures are called like "procedure(objectptr, params,...)", so there is only one procedure in memory ( <-> not one procedure for every instance).: : What you could do in this case is to pass the object (the instance - some people get confused with "class" and "object/instance" and ObjPas does it's best to "support" this confusion) and then call your "handle" procedure on that object instead of trying to pass the object's handle procedure itself.: : However, this would mean that you should use an interface (or something similiar in ObjPas). Like:: : [code]: THandler = object: procedure handle;: end;: : TMyObject = object(THandler): procedure handle; override;: ...: end;: [/code]: : Your method where you are registering the handler:: : [code]: procedure registerHandler(handler : THandler);: ...: [/code]: : And finally call the handler:: : [code]: handler.handle;: [/code]: : I'm not sure about the syntax in ObjPas under TP, it's too long ago since I'd used that, so you might have to alter it a bit :-(: : tron.:

Just one other thing, which might be interesting: in OOP, this is called the "Listener-Design-Pattern". As you might already guessed, the "handlers" are called "listeners" - interfaces which provide at least one method to "listen" to a certain "event".