我有一个 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";
}
Run Code Online (Sandbox Code Playgroud)
小智 7
当您取消引用 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";
}
Run Code Online (Sandbox Code Playgroud)
当您取消引用迭代器 i (即*i)时,您将获得对指针类型的对象的引用MyObject*,您必须再次取消引用才能获得对对象的引用:
*(*i)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23040 次 |
| 最近记录: |