简体   繁体   English

获取对象指针指向

[英]Get The Object Pointer Is Pointing To

I have a QList which I have inserted pointers of objects into. 我有一个QList,我已插入对象的指针。 I am trying to iterate through this QList to read the name of these objects. 我试图遍历此QList以读取这些对象的名称。 Whenever I do so, I am getting the address of these objects as oppose to reading the object name itself. 每当我这样做,我得到这些对象的地址,而不是阅读对象名称本身。 I am wondering how would I be able to read the object name instead of the address? 我想知道如何才能读取对象名称而不是地址?

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";
}

When you're dereferencing i, you need to call the function to get the name from your object. 当您取消引用i时,需要调用该函数以从对象中获取名称。 Something like this: 像这样的东西:

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";
}

当您取消引用迭代器i(即*i )时,您引用了一个MyObject*类型的对象,它是一个指针,您必须再次取消引用它以获取对象的引用:

*(*i)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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