传递函数返回void *作为参数

时间:2017-10-27 15:00:00

标签: c++ c++11 pointers function-pointers void-pointers

我想传递一个函数作为参数并将void *返回给另一个函数。 现在我有以下代码:

MyClass *MyClass::bind(std::function<void*(void *)> fun) {
    this->value = fun(this->value);
    return this;
}

valuevoid *内的MyClass变量,我无法弄清楚如何将函数传递给我的函数bind
当我尝试传递一个像这样的函数的名称

auto *class = new MyClass();
class->bind(fooFunction);

我收到错误

no viable conversion from '<overloaded function type>' to 'std::function<void *(void *)>'

有没有办法用这个代码实现我想要的东西,或者是否存在更简单的方法?

1 个答案:

答案 0 :(得分:2)

您有多个<table> <thead><tr></tr></thead> <tbody><tr></tr></tbody> </table>重载。

使用只有一个重载(与签名匹配)的函数名称。

或者,将其包裹在lambda中:

fooFunction

并依赖无状态lambda隐式强制转换为函数指针。

相关问题