Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Delphi to C++ (procedure of object)

Posted on 2004-11-02
8
Medium Priority
?
310 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
Comment
Question by:__alex
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 12471451
0
 
LVL 2

Author Comment

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

Assisted Solution

by:Cayce
Cayce earned 200 total points
ID: 12473894
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 2

Author Comment

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

Accepted Solution

by:
andrewjb earned 800 total points
ID: 12481533
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
ID: 12481886
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
ID: 12481948
0
 
LVL 12

Expert Comment

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

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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

604 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