Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4095
Number of posts: 14004

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Why isn't compatible procedure and method pointer? Posted by Mr. Ego on 1 Apr 2003 at 5:23 AM
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

Report
Re: Why isn't compatible procedure and method pointer? Posted by tron on 1 Apr 2003 at 9:13 AM
: 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:

  THandler = object
    procedure handle;
  end;

  TMyObject = object(THandler)
    procedure handle; override;
    ...
  end;


Your method where you are registering the handler:

  procedure registerHandler(handler : THandler);
  ...


And finally call the handler:

  handler.handle;


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.
Report
Re: Why isn't compatible procedure and method pointer? Posted by tron on 1 Apr 2003 at 9:18 AM
: : 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:
:
:
:   THandler = object
:     procedure handle;
:   end;
: 
:   TMyObject = object(THandler)
:     procedure handle; override;
:     ...
:   end;
: 

:
: Your method where you are registering the handler:
:
:
:   procedure registerHandler(handler : THandler);
:   ...
: 

:
: And finally call the handler:
:
:
:   handler.handle;
: 

:
: 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.
Report
Re: Why isn't compatible procedure and method pointer? Posted by Mr. Ego on 4 Apr 2003 at 2:38 AM
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.

type TProc = procedure;

     TSomething = object
       procedure DoSth;
     end;

procedure TSomething.DoSth;
begin

end;

procedure CallIt ( callproc : TProc );
begin
  callproc;
end;


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


Report
Re: Why isn't compatible procedure and method pointer? Posted by tron on 4 Apr 2003 at 3:28 AM
: 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.
:
:
: type TProc = procedure;
: 
:      TSomething = object
:        procedure DoSth;
:      end;
: 
: procedure TSomething.DoSth;
: begin
: 
: end;
: 
: procedure CallIt ( callproc : TProc );
: begin
:   callproc;
: end;
: 

:
: 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:

procedure CallIt ( callobj : TSomething );
begin
  callobj.DoSth;
end;


tron.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.