Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何从静态函数访问成员变量_C++ - Fatal编程技术网

C++ 如何从静态函数访问成员变量

C++ 如何从静态函数访问成员变量,c++,C++,我需要注册到第三方dll提供的回调函数,该函数不接受任何参数,因此我无法将“this”传递给它。这里的pData只是数据的一部分 //This is declared as static function in my class void CMyClass::ThirdPartyCallBack(void *pData) { //How to access the member variables of the class } 如何在不使用全局变量的情况下执行此操作,请注意,时间您可以创建

我需要注册到第三方dll提供的回调函数,该函数不接受任何参数,因此我无法将“this”传递给它。这里的pData只是数据的一部分

//This is declared as static function in my class
void CMyClass::ThirdPartyCallBack(void *pData)
{
  //How to access the member variables of the class
}

如何在不使用全局变量的情况下执行此操作,请注意,时间您可以创建一个上下文指针表,该表与一个“thunks”表关联,该表用作您给DLL的回调函数:

class foo {
public:
    void callback(void* data);
};

enum {
    kMAX_CALLBACKS = 3,
};

foo* context_table [kMAX_CALLBACKS] = {
        0,
        0,
        0

        // however many you might need...
};

extern "C" void callback_0( void* data)
{
        foo* ctx = context_table[0];

        ctx->callback(data);
}


extern "C" void callback_1( void* data)
{
        foo* ctx = context_table[1];

        ctx->callback(data);
}

extern "C" void callback_2( void* data)
{
        foo* ctx = context_table[2];

        ctx->callback(data);
}

// ....

extern "C" {
typedef void (*dll_callback)(void*);
}

dll_callback callback_table[kMAX_CALLBACKS] = {
        callback_0,
        callback_1,
        callback_2

        // however many you might need...
};


dll_callback set_callback_context(foo* ctx)
{
    for (int i =0; i < kMAX_CALLBACKS; ++i) {
        if (context_table[i] == 0) {
            context_table[i] = ctx;
            return callback_table[i];
        }
    }

    return 0;
}

void clear_callback_context(foo* ctx)
{
    for (int i =0; i < kMAX_CALLBACKS; ++i) {
        if (context_table[i] == ctx) {
            context_table[i] = 0;
            return;
        }
    }
}

回调将在
foo:callback()
处结束,并带有相应的
指针。

void*是指向您喜欢的任何对象的指针。您可以传递任何您喜欢的内容作为指向dll的数据指针,例如“this”。在回调中,将其转换回对象指针

我想他是说,
pData
是第三方DLL拥有并传递给回调函数的指针。
third_party_set_callback( set_callback_context(foo_instance_ptr));