Home > Backend Development > C++ > How to Safely Cast void* to a Pointer to Member Function in C ?

How to Safely Cast void* to a Pointer to Member Function in C ?

Patricia Arquette
Release: 2024-10-28 06:07:30
Original
697 people have browsed it

How to Safely Cast void* to a Pointer to Member Function in C  ?

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", &amp;Foo::bar);
Copy after login

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;
}
Copy after login

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;
}
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template