Pass a C++ member function to a C function

落爺英雄遲暮 提交于 2021-02-19 05:17:32

问题


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?


回答1:


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);


来源:https://stackoverflow.com/questions/52504511/pass-a-c-member-function-to-a-c-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!