Why isn't compatible procedure and method pointer? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Why isn't compatible procedure and method pointer?

Mr. EgoMr. Ego Posts: 38Member
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

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:

    [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.
  • : : 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".

    tron.
  • Mr. EgoMr. Ego Posts: 38Member
    I'm not sure that I understand this. For example:
    I have a function which I can't change. It takes one argument and this is procedure pointer.

    [code]
    type TProc = procedure;

    TSomething = object
    procedure DoSth;
    end;

    procedure TSomething.DoSth;
    begin

    end;

    procedure CallIt ( callproc : TProc );
    begin
    callproc;
    end;
    [/code]

    I can't change procedure CallIt. How can I give to the procedure CallIt address of TSomething.DoSth?


  • : I'm not sure that I understand this. For example:
    : I have a function which I can't change. It takes one argument and this is procedure pointer.
    :
    : [code]
    : type TProc = procedure;
    :
    : TSomething = object
    : procedure DoSth;
    : end;
    :
    : procedure TSomething.DoSth;
    : begin
    :
    : end;
    :
    : procedure CallIt ( callproc : TProc );
    : begin
    : callproc;
    : end;
    : [/code]
    :
    : I can't change procedure CallIt. How can I give to the procedure CallIt address of TSomething.DoSth?
    :
    :
    :

    You can not do it that way! But you can pass the object to CallIt and then call the procedure on the object:

    [code]
    procedure CallIt ( callobj : TSomething );
    begin
    callobj.DoSth;
    end;
    [/code]

    tron.
Sign In or Register to comment.