I want to use QtConcurrent::run()
for a member function, but it seems that it doesn't use the pointer to the instance. Instead, it looks like the default constructor is called
#include <QObject>
#include <QDebug>
#include <QtConcurrent>
class Foo : public QObject
{
Q_OBJECT
public:
Foo(int n = 0):n(n){}
Foo(const Foo & f):Foo(f.n){}
void foo(){qDebug() << "Foo " << n;}
void bar(){QtConcurrent::run(this, &Foo::foo);}
private:
int n;
};
void test(){
Foo foo = Foo(2);
foo.foo();
foo.bar();
QtConcurrent::run(&foo, &Foo::foo);
QtConcurrent::run(&foo, &Foo::bar);
}
And the results of running test()
are :
Foo 2
Foo 0 // Should be a 2
Foo 0 // Should be a 2
Foo 0 // Should be a 2
Edit : My instance indeed went out of scope. This code works fine
void test(){
Foo * foo = new Foo(2);
foo->foo();
foo->bar();
QtConcurrent::run(foo, &Foo::foo);
QtConcurrent::run(foo, &Foo::bar);
}