Casting Difficulties: Void* and Pointer to Member Functions
In the realm of programming, casting between different data types often presents challenges, especially when dealing with pointers to member functions. This article delves into an issue encountered while trying to create a C library that interfaces with a Lua interpreter, where converting void* to a pointer to member function posed a significant hurdle.
The code in question attempts to register a C object as a LuaObject and add a member function to that object:
template <class T> LuaObject<T> lobj = registerObject(L, "foo", fooObject); lobj.addField(L, "bar", &Foo::bar);
However, the following function causing the issue:
template <class T> int call_int_function(lua_State *L) { // problematic line void (T::*method)(int, int) = reinterpret_cast<void (T::*)(int, int)>(lua_touserdata(L, lua_upvalueindex(1))); T *obj = reinterpret_cast<T *>(lua_touserdata(L, 1)); (obj->*method)(lua_tointeger(L, 2), lua_tointeger(L, 3)); return 0; }
The issue lies in the compiler's complaint: it cannot cast void to a pointer to a member function. Pointers to members are specific and cannot be directly converted to regular pointers like void.
To resolve this issue, the solution is to wrap the member function in a regular function instead of using a pointer to member. This approach involves creating a free function that accepts the object as its first argument and calls the member function internally.
Here's a modified version of the function using this approach:
template <class T> int call_int_function(lua_State *L) { void (*method)(T*, int, int) = reinterpret_cast<void (*)(T*, int, int)>(lua_touserdata(L, lua_upvalueindex(1))); T *obj = reinterpret_cast<T *>(lua_touserdata(L, 1)); method(obj, lua_tointeger(L, 2), lua_tointeger(L, 3)); return 0; }
By wrapping the member function, we avoid the need to cast void* to a pointer to a member function, resolving the issue encountered while casting between these two data types.
The above is the detailed content of How to Safely Cast void* to a Pointer to Member Function in C ?. For more information, please follow other related articles on the PHP Chinese website!