4

We have a structure that accepts C function pointers:

int one(int x)
{
}

int two(int x)
{
}

struct Cstruct
{
    int (*fn1)(int);
    int (*fn2)(int);
};

Now I have a C++ class that has below methods:

class A
{
public:
    int one(int x)
    {
    }

    int two(int x)
    {
    }

    int three(int x)
    {
        struct Cstruct cstr = {&this->one, &this->two};
    }
};

While trying to initialize class A methods address to a instance of Cstruct compiler is giving error of an invalid conversion?

How can I assign the Class member function address to Cstruct?

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
Programmer
  • 8,303
  • 23
  • 78
  • 162
  • Member functions pointers != function pointers. – Quimby Sep 25 '18 at 18:21
  • 2
    Member functions are *not* equal to non-member functions. The big difference being that (non-static) member functions needs an *object* to be called on. Therefore they can't be used as pointers to non-member functions, and passed to C functions for callbacks. And no, using `this` when getting the pointer won't help, as it's still a pointer to the *function only*. – Some programmer dude Sep 25 '18 at 18:21
  • Function pointers are different from member function pointers. You want to use the syntax `int (A::*fn1)(int);`. You will also need an instance of `A` to call the function from. – 0x5453 Sep 25 '18 at 18:22
  • Member functions have a hidden `this` as first parameter, so the type of `A::one` actually looks something like `int (*fn)(A*, int)` – James Poag Sep 25 '18 at 18:23
  • There *could* be a way around the problem, if the structure have some kind of user-data member or pointer. Then you could set that pointer to the object, and have a function-pointer to a non-member function (or a `static` member function) which takes the user-data pointer and cast it to the proper type and call the actual member function. – Some programmer dude Sep 25 '18 at 18:24
  • [Wrapping C++ class API for C consumption](https://stackoverflow.com/a/1590534/14065) – Martin York Sep 25 '18 at 19:12

1 Answers1

8

You cannot do it, because C++ pointer to a non-static member function is not compatible with a non-member function pointer type. This is because member functions require an additional argument - the object on which the member function needs to be called, which becomes this pointer inside the invocation.

If you make your member functions static, your code would compile. However, it would not necessarily do what you want to achieve, because one and two have no access to other non-static members of A.

A trick to passing member functions to C functions requires passing an additional void* pointer with the "registration" record, and having C code pass it back to your static callback functions:

struct Cstruct
{
    void *context; // Add this field
    int (*fn1)(void*, int);
    int (*fn2)(void*, int);
};

class A
{
public:
    static int oneWrap(void* ptr, int x)
    {
        return static_cast<A*>(ptr)->one(x);
    }

    static int twoWrap(void* ptr, int x)
    {
        return static_cast<A*>(ptr)->two(x);
    }

    int one(int x)
    {
    }

    int two(int x)
    {
    }

    int three(int x)
    {
        struct Cstruct cstr = {this, &this->oneWrap, &this->twoWrap};
    }
};

C code would need to pass the value of context to fn1 and fn2:

cs.fn1(cs.context, 123);
cs.fn2(cs.context, 456);
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523