我有一个 QList,我已将对象指针插入其中。我正在尝试遍历此 QList 以读取这些对象的名称。每当我这样做时,我都会获取这些对象的地址,而不是读取对象名称本身。我想知道如何才能读取对象名称而不是地址?
QList<MyObject*> newObjectList;
QList<MyObject*>::iterator i;
MyObject *pNewObject = new MyObject(name);
MyObject.append(pNewObject);
for (i = newObjectList.begin(); i != newObjectList.end(); i++) {
cout << "\n" << *i << "\n";
}
最佳答案
当您取消引用 i 时,您需要调用该函数以从您的对象中获取名称。像这样:
for (i = newObjectList.begin(); i != newObjectList.end(); i++) {
// i right now is the iterator, and points to a pointer. So this will need to be
// dereferenced twice.
cout << "\n" << (*i)->getName() << "\n";
}
关于c++ - 获取指针指向的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9333526/