Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a C++ member function to a C function

Tags:

c++

c

c++11

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?

like image 207
Programmer Avatar asked Jan 27 '23 09:01

Programmer


1 Answers

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);
like image 78
Sergey Kalinichenko Avatar answered Jan 30 '23 04:01

Sergey Kalinichenko