How to pass a method pointer to a struct (in C/C++)? -


I have a C + + class in which there is a pointer that represents something like this class's C interface. Actually everything works well in addition to one thing: I can not cross the method indicator in this structure in the Macloss constructor. The example implementation looks like the following:

  Pure purifier code typingf int ci_function (ci_struct *); Typedef struct CI_MyClass // C MyClass's interface {CI_AFunction * func; } CI_MyClass; // C ++ Code Class MyClass {Private: CI_Afunction * _func; CI_MyClass * ptrCI_MyClass; Public: MyClass (): _func (** What to do to point it to MyFunc? **) {ptrCI_MyClass = New CI_MyClass; PtrCI_MyClass-> Function = _func; } Int'l MyFunk (CI_Start * str) {// Some Return 0; } CI_MyClass * GetCInterface () {return ptrCI_MyClass; }}; // More or less usage looks like this: CI_ptr-> Fank (CI_strust vs. PTR); // In a separate file   

How to solve it? I'm not sure that such an approach is also possible, although solving this method would be the most frightening, even if it is not elegant. This "C interface" is considered to reveal only one code in C code, so I am not trying to create a fully programming-practice C API.

EDIT: Thanks for all the people for help. Your signal really lets me solve the problem. I have created a singleton which is not a bad attitude for the manager of something. Now I have stable work and everything works perfectly Thanks!

Points are incompatible, you may be able to store the indicator but you can not call it What do you want to do, this is to put a student to the pedal, they work differently. I see 2 potential solutions here.

  1. If MyFunc is marked as Fixed , then it behaves like a stdcall, but then you use You can not do this in the method and you are obliged to use CI_Struct * str instead.

    When the method is marked as static, you can put it in a regular C function

      _func (CI_Afunction) and MyClass :: MyFunc )  
  2. The 'bridge' between them will make the C call function able to call the object on an object (C ++ ish) would be required. Some kind

      int adapter (int (microse :: * pemphth) (ci_strat *), miracles * obje (return obj-> pMethod (obj-> GetCInterface ()); / * Or third paragraph which is the structure * /}     

    Option 1 appears as you are looking at, because you have passed the structure because I All data should be understood.

Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -