简体   繁体   中英

QList request item from array not giving correct reference

Apologies if the title is incorrectly phrased - I am not too sure what is causing the problem

I am testing QList array access and came across this. It is a straight forward example of using referencing QList append() function and QList[] operator.

Aim: I am trying to find out whether adding the same object (created with new ) to 2 QList<int> and changing one of those objects (or references rather) will change the other.

What I found seems to indicate this is not true given my example and the output below:

// Some structure to simluate an object
struct IntStream {
    int i;
};

// Create our lists
QList<IntStream> newlist = QList<IntStream>();
QList<IntStream> another = QList<IntStream>();

// Add 3 IntStream objects to the 2 lists using the same object, printing out the object and its reference
for (int i = 0; i < 3; i++) {
    IntStream *s = new IntStream;
    s->i = i;
    newlist.append(*s);
    another.append(*s);
    qDebug() << QString("%1[%2] = %3 (").arg("newList", QString::number(i), QString::number(i)) << &another[i] << ")";
    qDebug() << QString("%1[%2] = %3 (").arg("another", QString::number(i), QString::number(i)) << &another[i] << ")";
}

// Alter bject at index 1 with some arbitrary value
for (int i = 0; i < 3; i++) {
    if(newlist.at(i).i == 1) {
        qDebug() << "another[1] = " << &another[i];
        qDebug() << "newList[1] = " << &newlist[i];
        another[i].i = 4;
    }
}

// Here, I should see the 2 values match, they do not
qDebug() << QString("%1 == %2 ???").arg(QString::number(newlist.at(1).i), QString::number(another.at(1).i));

The output of this is:

"newList[0] = 0 (" 0x27c75f88 )
"another[0] = 0 (" 0x27c75f88 )
"newList[1] = 1 (" 0x27c755d0 )
"another[1] = 1 (" 0x27c755d0 )
"newList[2] = 2 (" 0x27c75630 )
"another[2] = 2 (" 0x27c75630 )
another[1] =  0x27c755d0
newList[1] =  0x27c76ef0
"1 == 4 ???"

Should I expect to see 4 == 4 or did I do something wrong somewhere?

Notes:

  • I am using the T &QList::operator[](int i) , not const T &QList::operator[](int i) const
  • Creating new objects rather than storing scoped objects
qDebug() << QString("%1[%2] = %3 (").arg("newList", QString::number(i), QString::number(i)) << &another[i] << ")";
qDebug() << QString("%1[%2] = %3 (").arg("another", QString::number(i), QString::number(i)) << &another[i] << ")";

You compared twice the &another[i]. You should write &newlist[i] instead in the first line.

And when you call newlist.append(*s); you made a copy of your IntStream instance.

To answer to your need: " I am trying to find out whether adding the same object (created with new) to 2 QList and changing one of those objects (or references rather) will change the other. " Use a shared_ptr to shared your instance between multiple list.

Something like:

struct IntStream {
    int i;
};

// Create our lists
QList<std::shared_ptr<IntStream >> newlist = QList<std::shared_ptr<IntStream >>();
QList<std::shared_ptr<IntStream >> another = QList<std::shared_ptr<IntStream >>();

// Add 3 IntStream objects to the 2 lists using the same object, printing out the object and its reference
for (int i = 0; i < 3; i++) {
    std::shared_ptr<IntStream > s = std::make_shared<IntStream >();
    s->i = i;
    newlist.append(s);
    another.append(s);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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