指向类方法 QT 的指针向量
Vector of pointers to class methods QT
如何创建指向类方法的指针向量?我将我的向量作为类的成员(向量必须存储具有不同返回值和签名的方法指针):
QVector<void(*)()> m_operationsVector;
然后我有示例类的方法:
QString sampleMethod(QJsonObject &jsonObject, QString delim);
我正在尝试将此方法的指针添加到矢量:
m_operationsVector.push_back(sampleMethod);
但不幸的是,在将此指针添加到矢量的过程中,我收到此错误:
error: invalid use of non-static member function
如何解决此问题?
首先,指向类方法的指针定义不同,因此此向量应如下所示:
QVector<void (A::*)()> m_operationsVector;
其次,在 C++11 中使用 std::function
和 lambda 更方便:
QVector<std::function<void()>> m_operationsVector;
operationsVector.push_back([this]() { this->someMethod(); });
第三,当这与JSon相结合时,这看起来很可疑。你在干什么?这看起来像 XY 问题。
相关文章:
- 有没有一种"cleaner"的方法可以在指向基的指针向量中找到派生类的第一个实例?
- 如何更改唯一指针向量的可见性
- 如何在基类指针向量的元素上应用重载的多态函数
- C++:添加新结构时,结构指针向量中的所有元素都会更新
- 包含指向其他结构的指针向量的结构
- 如何将子类作为函数的参数传递给期望基类,然后将该对象传递到指向这些抽象类对象的指针向量中?
- 如何将字符串指针数组转换为字符串类型的智能指针向量?
- 初始化可变数据结构中的共享指针向量
- 指向具有 decltype 的函数的指针向量
- push_back指向指针向量的指针
- 使用指针向量(带代码)C++(以及对其使用的便利性的怀疑)时出现问题
- 我可以做些什么来改进指针向量中的此搜索?
- 指向指针排序向量的指针向量的 C++ 向量
- 初始化用户定义类的指针向量
- 函数无法识别我在C++传递节点指针向量?
- 如何声明指向类对象的指针向量?
- 指向结构的指针向量的元素具有相同的地址
- 删除指向抽象类的指针向量
- 当我返回指向结构的指针向量时出现段错误
- 用基于范围的for循环填充指针向量