简体   繁体   English

如何从 2 个 void 指针调用 class 成员 function

[英]How to call a class member function from 2 void pointers

So for this function:所以对于这个 function:

template<typename T>
T popObject(ScriptObject obj) { //Preforms a cast from void* on each to its type.
    assert(obj.getType() == std::type_index(typeid(T))); //Ensure type Safety.
    return obj.get<T>();
}

template <typename C, typename...Args>
    void bind(const char* name, C* context, void(C::* function)(Args...)) {
        std::vector<std::type_index> arguments = { std::type_index(typeid(Args))... };
        auto functor = [](void* func,void* contex, std::vector<ScriptObject> stack) {
            std::size_t idx = 0;
            //Call class context -> function (Args...)
        };
        _functions[name] = new BoundFunction(functor, void_cast(function), context, arguments);
    }

I'm unsure of the syntax for calling the member function.我不确定调用成员 function 的语法。

Iv tried:四试过了:

union MF{
    void(C::* pf)(Args...);
    void* p;
} mf;
mf.p = func;
contex->*(mf.pf)(popObject<Args>(stack[idx++])...);

Which does not compile, Stating: "Does not evaluate to a function taking 1 Arguments."哪个不编译,说明:“不评估为 function 采取 1 Arguments。”

Tried std::invoke, But I Don't know how to Specify the Member function Signature for arg1.尝试了 std::invoke,但我不知道如何为 arg1 指定成员 function 签名。

So If i have a reference to the member function, And a reference to the instance of the class, As well as the template information to Derive type information.因此,如果我有对成员 function 的引用,以及对 class 实例的引用,以及派生类型信息的模板信息。 How do I call the Classes member function?我如何称呼班级成员 function?

Thanks @Igor Tandentnik谢谢@Igor Tandentnik

Final Solution was:最终解决方案是:

(static_cast<C*>(ctx)->*(mf.pf))(popObject<Args>(stack[idx++])...);

As Igor pointed out, it needed to be ->* instead of.*, and added parenthesis, as -> has a lower priority then the Function call operator.正如 Igor 指出的那样,它需要是 ->* 而不是.*,并添加括号,因为 -> 的优先级低于 Function 调用运算符。

Also, thanks @TheFloatingBrain for pointing out the extra static_cast另外,感谢@TheFloatingBrain 指出额外的 static_cast

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM