小编mov*_*vef的帖子

Qt并发运行,通过引用传递值,但内存地址不同?

我QtConcurrent::run用来运行一个函数,并通过引用传递值,但值的内存地址是不同的.

但是如果我通过指针传递值,地址是一样的!我无法弄清楚.我错过了什么吗?

这是代码.

void ptr(QString* s)
{
    qDebug() << "pass by ptr: " << s;
}

void ref(QString& s)
{
    qDebug() << "pass by ref: " << &s;
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QString str;
    QFuture<void> f1 = QtConcurrent::run(ptr, &str);
    f1.waitForFinished();

    QFuture<void> f2 = QtConcurrent::run(ref, str);
    f2.waitForFinished();

    qDebug() << "address of str: " << &str;

    return a.exec();
}
Run Code Online (Sandbox Code Playgroud)

输出:

pass by ptr:  0x28fefc
pass by ref:  0x525de4
address of str:  0x28fefc
Run Code Online (Sandbox Code Playgroud)

c++ qt multithreading qtconcurrent

7
推荐指数
2
解决办法
2655
查看次数

标签 统计

c++ ×1

multithreading ×1

qt ×1

qtconcurrent ×1