问题
I am creating sort of a router for REST API in Qt and I am facing problem with inserting the function pointer into the QMap.
I have class IModule
from which the other modules are derivated.
Important lines of IModule.h
are
typedef QByteArray* (*IBusAction)(IBus * , ProxyRequest *);
class IModule : public QObject
{
Q_OBJECT
protected:
QMap<QString , IBusAction > *m_actions;
Then I have UserModule
which is derived from IModule
and in .cpp
file I have these lines:
QByteArray* UserModule::create(IBus *bus, ProxyRequest *req)
{
}
QByteArray* UserModule::show( IBus *bus, ProxyRequest *req)
{
}
UserModule::UserModule(QObject *parent) :
IModule(parent)
{
// register functions
m_actions->insert("show", &UserModule::show);
m_actions->insert("create", UserModule::create);
}
So I tried two options how to put the function in QMap with referencing sign also without it, but both are not working. I am getting error: no matching function for call to 'QMap<QString, QByteArray* (*)(IBus*, ProxyRequest*)>::insert(const char [5], QByteArray* (UserModule::*)(IBus*, ProxyRequest*))'
I have spent several hours with this problem, tried many different things how to solve it but there was no success.
So I will be very glad for any piece of advice.
回答1:
Your IBusAction
is a pointer-to-function type. This is not compatible with pointer-to-member function.
When you call a member function (like your UserModule::create
function), it needs an extra ("invisible") parameter: the instance the function gets called on (the this
pointer).
You have essentially three options:
Change
IBusAction
to be a pointer-to-member-function-of-UserModule
. You're restricted to that class's functions with this though.typedef QByteArray* (UserModule::*IBusAction)(IBus * , ProxyRequest *);
Make the functions
static
(this changes the semantics of your code)- Use free-standing functions (top-level, not members of a class) instead of member functions.
回答2:
Pointers to member functions in C++ is generally more hassle than it is worth. Sane alternative is to have class instead of method, and keep pointer to instance instead of method.
Qt specific alternative is to use invokable methods of QObject based class, which you can call by string name, using QObject::invokeMethod, side-stepping many type problems.
来源:https://stackoverflow.com/questions/13330806/insert-function-pointer-into-qmap-qt