Delphi to C++ (procedure of object)

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);
LVL 2
__alexAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kretzschmarCommented:
0
__alexAuthor Commented:
Interesting, indeed...
0
CayceCommented:
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
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

__alexAuthor Commented:
Hi Cayce,
I don't want my Bar.Foo method to be static :-(
0
andrewjbCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
__alexAuthor Commented:
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
__alexAuthor Commented:
0
andrewjbCommented:
Have stored the link but not read yet...

Answer is templates if you've lots of class types.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.