Hi everybody,

I will be asking some confirmations instead of questions simple because even as a python developer I do not have (any apparent at least) problem with the code examples below. I wanted to create this topic to have my understanding of pointers as concrete as possible. I think along with myself, many people can benefit it.

I will be given examples topic by topic, again, all codes below are compile(not sure leaks though). Here I go.

1. Pointers point
Yeah, we know that. However, if I delete a pointer which points to an object such as QGraphicsItem, it deletes that object.

Qt Code:
  1. QList <QGraphicsItem*> items = this->graphScene->items();
  2. foreach(QGraphicsItem *i, items)
  3. {
  4. MyGraphItem *graphItem = qgraphicsitem_cast< MyGraphItem *>(i);
  5. // I did stuff with graphItem here now it’s time to delete it
  6. // not the object it points to though
  7. }
To copy to clipboard, switch view to plain text mode 

If I delete it like this;

Qt Code:
  1. delete graphItem;
To copy to clipboard, switch view to plain text mode 

it deletes the graph item it points to. I just want to delete (remove from the memory) graphItem. Not the item it points to. So I am deleting it like this;

Qt Code:
  1. graphItem = NULL;
  2. delete graphItem;
To copy to clipboard, switch view to plain text mode 

I was wondering whether this way is the right way to delete the pointer while not touching the object it points to.



2. Returning Pointers
Is the approach I am using below a right one?
Qt Code:
  1. MyGraphItem *GraphView::getItem(const QString &name)
  2. {
  3. QList <QGraphicsItem*> items = this->graphScene->items();
  4. if(items.isEmpty())
  5. return NULL;
  6.  
  7. foreach(QGraphicsItem *i, items)
  8. {
  9. // Here i am allocating memory
  10. MyGraphItem *graphItem = qgraphicsitem_cast<MyGraphItem *>(i);
  11. if(typeid(*graphItem ) == typeid(MyGraphItem ))
  12. {
  13. // this is the object I want so return it
  14. if(graphItem ->name == name)
  15. {
  16. return graphItem ;
  17. }
  18. }
  19. // Hovewer if the object *graphItem is not wat I wanted
  20. // Should I delete it here, like so
  21. graphItem = NULL;
  22. delete graphItem;
  23. }
  24. return NULL;
  25. }
To copy to clipboard, switch view to plain text mode 

What about the returning a string below. I assume the receiving method should delete the pointer when it's no longer needed, right?
Qt Code:
  1. QString *GraphAttribute::getItemName()
  2. {
  3. MyGraphItem *graphItem = qgraphicsitem_cast<MyGraphItem *>(this->parentItem());
  4. return &graphItem ->name;
  5. }
To copy to clipboard, switch view to plain text mode 


Another confirmation for returning pointer.

Qt Code:
  1. GraphData *GraphItem::getAttribute(const QString &dataName)
  2. {
  3. // Here I have a QList that contains pointers of GraphData instances
  4. if(this->data.isEmpty())
  5. return NULL;
  6.  
  7. foreach(GraphData *d, this->data) // Getting them individually
  8. {
  9. if(d->data.name == dataName)
  10. return attr; // This is what I want, return it, this return pointer must be deleted
  11. // in caller method when it's no longer needed, right.
  12. else
  13. {
  14. // This is not what I want delete it
  15. d = NULL;
  16. delete d;
  17. }
  18. }
  19. return false;
  20. }
To copy to clipboard, switch view to plain text mode 


Would you please let me know if you see any problem in my code?

Thanks