简体   繁体   English

指向类方法QT的指针向量

[英]Vector of pointers to class methods QT

How to create a vector of pointers to class methods? 如何创建指向类方法的指针的矢量? I have my vector as class's member (vector must store pointers of methods with different return values and signatures): 我将vector作为类的成员(vector必须存储具有不同返回值和签名的方法的指针):

QVector<void(*)()> m_operationsVector;

Then I have example class's method: 然后我有示例类的方法:

QString sampleMethod(QJsonObject &jsonObject, QString delim);

And I'm trying to add pointer of to this method to vector: 我正在尝试将指向此方法的指针添加到矢量:

m_operationsVector.push_back(sampleMethod);

But unfortunately during adding this pointer to vector I'm getting this error: 但是不幸的是,在将此指针添加到vector的过程中,我遇到了以下错误:

error: invalid use of non-static member function

How can I fix this issue? 如何解决此问题?

First of all pointer to class method is defined differently, so this vector should look like this: 首先,指向类方法的指针的定义不同,因此此向量应如下所示:

QVector<void (A::*)()> m_operationsVector;

Secondly in C++11 it is more handy to use std::function and lambdas: 其次,在C ++ 11中,使用std::function和lambdas更方便:

QVector<std::function<void()>> m_operationsVector;

operationsVector.push_back([this]() { this->someMethod(); });

Thirdly when this is combined with JSon this looks suspicious. 第三,当它与JSon结合使用时,这看起来很可疑。 What are you doing? 你在做什么? This looks like XY Problem . 这看起来像XY问题

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

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