devla
30th October 2012, 05:26
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.
QList <QGraphicsItem*> items = this->graphScene->items();
foreach(QGraphicsItem *i, items)
{
MyGraphItem *graphItem = qgraphicsitem_cast< MyGraphItem *>(i);
// I did stuff with graphItem here now it’s time to delete it
// not the object it points to though
}
If I delete it like this;
delete graphItem;
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;
graphItem = NULL;
delete graphItem;
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?
MyGraphItem *GraphView::getItem(const QString &name)
{
QList <QGraphicsItem*> items = this->graphScene->items();
if(items.isEmpty())
return NULL;
foreach(QGraphicsItem *i, items)
{
// Here i am allocating memory
MyGraphItem *graphItem = qgraphicsitem_cast<MyGraphItem *>(i);
if(typeid(*graphItem ) == typeid(MyGraphItem ))
{
// this is the object I want so return it
if(graphItem ->name == name)
{
return graphItem ;
}
}
// Hovewer if the object *graphItem is not wat I wanted
// Should I delete it here, like so
graphItem = NULL;
delete graphItem;
}
return NULL;
}
What about the returning a string below. I assume the receiving method should delete the pointer when it's no longer needed, right?
QString *GraphAttribute::getItemName()
{
MyGraphItem *graphItem = qgraphicsitem_cast<MyGraphItem *>(this->parentItem());
return &graphItem ->name;
}
Another confirmation for returning pointer.
GraphData *GraphItem::getAttribute(const QString &dataName)
{
// Here I have a QList that contains pointers of GraphData instances
if(this->data.isEmpty())
return NULL;
foreach(GraphData *d, this->data) // Getting them individually
{
if(d->data.name == dataName)
return attr; // This is what I want, return it, this return pointer must be deleted
// in caller method when it's no longer needed, right.
else
{
// This is not what I want delete it
d = NULL;
delete d;
}
}
return false;
}
Would you please let me know if you see any problem in my code?
Thanks
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.
QList <QGraphicsItem*> items = this->graphScene->items();
foreach(QGraphicsItem *i, items)
{
MyGraphItem *graphItem = qgraphicsitem_cast< MyGraphItem *>(i);
// I did stuff with graphItem here now it’s time to delete it
// not the object it points to though
}
If I delete it like this;
delete graphItem;
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;
graphItem = NULL;
delete graphItem;
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?
MyGraphItem *GraphView::getItem(const QString &name)
{
QList <QGraphicsItem*> items = this->graphScene->items();
if(items.isEmpty())
return NULL;
foreach(QGraphicsItem *i, items)
{
// Here i am allocating memory
MyGraphItem *graphItem = qgraphicsitem_cast<MyGraphItem *>(i);
if(typeid(*graphItem ) == typeid(MyGraphItem ))
{
// this is the object I want so return it
if(graphItem ->name == name)
{
return graphItem ;
}
}
// Hovewer if the object *graphItem is not wat I wanted
// Should I delete it here, like so
graphItem = NULL;
delete graphItem;
}
return NULL;
}
What about the returning a string below. I assume the receiving method should delete the pointer when it's no longer needed, right?
QString *GraphAttribute::getItemName()
{
MyGraphItem *graphItem = qgraphicsitem_cast<MyGraphItem *>(this->parentItem());
return &graphItem ->name;
}
Another confirmation for returning pointer.
GraphData *GraphItem::getAttribute(const QString &dataName)
{
// Here I have a QList that contains pointers of GraphData instances
if(this->data.isEmpty())
return NULL;
foreach(GraphData *d, this->data) // Getting them individually
{
if(d->data.name == dataName)
return attr; // This is what I want, return it, this return pointer must be deleted
// in caller method when it's no longer needed, right.
else
{
// This is not what I want delete it
d = NULL;
delete d;
}
}
return false;
}
Would you please let me know if you see any problem in my code?
Thanks