Solved

Delphi to C++ (procedure of object)

Posted on 2004-11-02
284 Views
Last Modified: 2010-04-05
I wonder how the following snippet will appear in C++.

type
  TFoo = procedure(i: integer) of object;
var
  foo: TFoo;
...
foo := Bar.Foo;
foo(2);
0
Question by:__alex
    8 Comments
     
    LVL 27

    Expert Comment

    by:kretzschmar
    0
     
    LVL 2

    Author Comment

    by:__alex
    Interesting, indeed...
    0
     
    LVL 9

    Assisted Solution

    by:Cayce
    Something like this:

    class Bar
    {
    public:
      static void Foo(int i) {}
    };

    typedef void (*TFoo)(int i);
    /*...*/
    void main()
    {
      TFoo foo;
      Bar a;
      foo = &a.Foo;
      foo(2);
    }
    0
     
    LVL 2

    Author Comment

    by:__alex
    Hi Cayce,
    I don't want my Bar.Foo method to be static :-(
    0
     
    LVL 12

    Accepted Solution

    by:
    You can't directly. You can store a pointer to a class function, but to call it you need to supply the class instance too.

    Roughly like:

    class ToCall
    {
      void SomeFunction( int x ) {...}
    };

    typedef void (ToCall::*FnType)(int);

    FnType MyPointer = &ToCall::SomeFunction;

    then

    ToCall x;

    (x.*MyPointer)(10);



    So, if you want to store it in a Delphi-like way, you need a class which stores a) the class instance, and b) the pointer reference

    class tMyDelphiLikePointer
    {
     typedef void (ToCall::*FnType)(int);
      FnType   fFn;
      ToCall *fInstance;

      void CallIt( int a ) { (ToCall.*fFn)(a); };
    };

    (Roughly! Havne't tried compiling these!)

    .. and there might be an alternative that I don't know about yet!

    0
     
    LVL 2

    Author Comment

    by:__alex
    Ok, got it. But I need a bunch of classes if I have different "target object" types or use templates, do I?
    foo := Bar_1.Foo;   // Bar_1 is TBar_1
    foo := Bar_2.Foo;   // Bar_2 is TBar_2
    That gets complicated...
    0
     
    LVL 2

    Author Comment

    by:__alex
    0
     
    LVL 12

    Expert Comment

    by:andrewjb
    Have stored the link but not read yet...

    Answer is templates if you've lots of class types.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Anonabox PRO Tor & VPN Router

    PRO is the most advanced way to fortify your privacy and online anonymity by layering the Tor network with VPN services. Use both together or separately, and without needing to download software onto your devices.

    A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
    Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
    This video discusses moving either the default database or any database to a new volume.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    856 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now