drewpol
drewpol

Reputation: 697

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):

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:

error: invalid use of non-static member function

How can I fix this issue?

Upvotes: 0

Views: 415

Answers (1)

Marek R
Marek R

Reputation: 37647

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:

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

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

Thirdly when this is combined with JSon this looks suspicious. What are you doing? This looks like XY Problem.

Upvotes: 1

Related Questions