Get a pointer to an object points to

I have a QList in which I inserted object pointers. I am trying to go through this QList to read the name of these objects. Whenever I do this, I get the address of these objects as opposed to reading the name of the object itself. I am wondering how could I read the name of an object instead of an 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"; } 
+4
source share
2 answers

When you search for i, you need to call a function to get a name from your object. Something like that:

 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"; } 
+3
source

When you break the iterator i (i.e. *i ), you are referencing an object of type MyObject* , which is a pointer, you must dereference this again to get a reference to your object:

 *(*i) 
+2
source

Source: https://habr.com/ru/post/1397097/


All Articles