如何使用绑定将成员函数作为函数指针传递? [英] How do I use bind to pass a member function as a function pointer?

查看:408
本文介绍了如何使用绑定将成员函数作为函数指针传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将成员函数作为函数指针传递,这样我就不必依赖单例或全局函数来处理Qt 5中的Qt消息.据我所知,std :: function是正确类型的对象,它具有正确的签名,并且绑定应该使我无法使用隐式this指针,实质上是将成员函数作为全局/非拥有函数传递出去.

I'm trying to pass a member function as a function pointer so that I don't need to rely on singletons or global functions to handle Qt messages in Qt 5. As far as I can tell my std::function is of the correct type, it has the correct signature, and bind should be allowing me to jam in the implicit this pointer, essentially passing off a member function as a global/un-owned function.

void ProgramMessageHandler::setAsMessageHandlerForProgram() {
    std::function<void(QtMsgType, const QMessageLogContext &, const QString &)> funcPtr;

    funcPtr = std::bind(handleMessages, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);

    qInstallMessageHandler(funkPtr);
}

这不能编译.我可以成功创建我的funcPtr变量,但是将其传递给qInstallMessageHandler函数会导致以下情况:

This doesn't compile. I can succesfully create my funcPtr variable but passing it into the qInstallMessageHandler function causes the following:

log\ProgramMessageManager.cpp:16: error: cannot convert 'std::function<void(QtMsgType, const QMessageLogContext&, const QString&)>' to 'QtMessageHandler {aka void (*)(QtMsgType, const QMessageLogContext&, const QString&)}' for argument '1' to 'void (* qInstallMessageHandler(QtMessageHandler))(QtMsgType, const QMessageLogContext&, const QString&)'
 oldHandler = qInstallMessageHandler(hackedPointerToHandleFunction);
                                                                  ^

我读过:

如何将成员函数作为函数传递指针?

如何传递成员函数指针?

在使用std ::时从std :: function获取函数指针绑定

但没有人帮助我.

因此,有人说没有单例或全局功能是不可能的……但是为什么呢?具有相同签名的成员函数和全局函数之间的唯一区别是,它们具有相同的签名,其中有一个隐式的this指针作为成员函数的第一个参数.

So it has been said that this isn't possible without a singleton or a global funtion... but why? The only difference between a member function and a global function with the same signature is that they don't have the same signature, there is an implicit this pointer as the first parameter to a member function.

知道,为什么我不能使用std::bind来弥合这种差距,所以说:我知道被调用的函数先于其他所有参数接受this参数,然后将其强加到那里,大约this.这比单例要干净得多,而全局函数只是胡扯.

Knowing that, why cannot I use std::bind to bridge this gap, by saying 'I know that the function being called takes the this argument before all of the others, force it in there, be the thing that knows about this. That would be so much cleaner than a singleton and a global function is just crap.

推荐答案

您正在尝试传递类实例,该类实例中应有功能指针,而没有进行此类转换存在.

You are trying to pass a class instance where a function pointer is expected, while no such conversion exists.

您可以执行以下操作:

class ProgramMessageHandler
{
  static void  myMessageHandler(QtMsgType, const QMessageLogContext &, const QString &);
};

在main.cpp中(例如):

in main.cpp (for example):

...
qInstallMessageHandler(ProgramMessageHander::myMessageHandler);
...

您可能仍然必须像在单例类中那样处理一些问题,但是我认为有一个消息处理程序或至少一个调度程序可以以某种方式将不同的消息重定向到适当的处理程序是有意义的.希望这会有所帮助

You still might have to deal with some issues as in singleton classes, but I think it makes sense that there is one message handler or at least one dispatcher that would somehow redirect different message to appropriate handlers. Hope this helps

这篇关于如何使用绑定将成员函数作为函数指针传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆