best way to delete QVector<QVector3D>
What is the best way to delete
Code:
QVector<QVector3D> vertices
I already cleared it. Now for deleting it, I tried to use qDeleteAll() function, but
it asks for begin and end iterators. I do not know how to iterate through all vectors and call qDeleteAll(). Nor do I know if that is really necessary.
On the other hand, vertices.reserve(0) also does not delete the whole thing. (I don't think so)
what's your idea?
Re: best way to delete QVector<QVector3D>
Don't do anything. Let it go out of scope and all will be done for you.
Re: best way to delete QVector<QVector3D>
but what if it doesn't go out of scope. I need to delete after right after drawing.
Code:
qDeleteAll(vertices.begin(), vertices.end());
gives the following error:
Quote:
error: type 'class QVector3D' argument given to 'delete', expected pointer
Code:
template <typename ForwardIterator>
Q_OUTOFLINE_TEMPLATE void qDeleteAll(ForwardIterator begin, ForwardIterator end)
{
while (begin != end) {
delete *begin;
++begin;
}
}
Re: best way to delete QVector<QVector3D>
Quote:
Originally Posted by
saman_artorious
but what if it doesn't go out of scope. I need to delete after right after drawing.
Clear the vector.
Re: best way to delete QVector<QVector3D>
Quote:
Originally Posted by
wysota
Don't do anything. Let it go out of scope and all will be done for you.
in this case it will be overloaded. not deleted i guess.
Re: best way to delete QVector<QVector3D>
What will be overloaded? Overloaded with what?
Re: best way to delete QVector<QVector3D>
Quote:
Originally Posted by
wysota
What will be overloaded? Overloaded with what?
if by letting it go out of scope you mean to push to it. when it reaches the boundary of allocation, it starts from the beginning overloading.
Re: best way to delete QVector<QVector3D>
I'm sorry, I don't understand what you mean.
Re: best way to delete QVector<QVector3D>
Hi,
What wysota want to tell you is that:
Code:
void myClass::myMethod()
{
QVector<QVector3D> vector;
... //use of the vector
}
When the method call exits, the QVector<QVector3D> will be cleared.
If you want to delete the vector when it still is in the scope you have to call clear:
Code:
void myClass::myMethod()
{
QVector<QVector3D> vector;
... //use of the vector
vector.clear(); //This clears the vector
... //Here you can reuse the vector with new data
}
Re: best way to delete QVector<QVector3D>
A scope or block scope is a visiblity and validiity construct of C and languages with similar syntax such as C++, Java, etc.
Names, e.g. variables, declared in a scope are usually only visible within the scope and nested scopes, not outside, i.e. not in the parent scope or sibling scopes.
Languages such as C and C++, which allow data on the stack, destroy such data when the variable's scope ends.
Code:
int main()
{
// inside scope of main()
int a;
a = 3; // valid
{
int b;
// inside a nested scope
a = 4; // valid
b= 4; // valid
}
a = 5; // valid;
b = 5; // will not compile, b only visible/valid in nested scope
}
In C++, an object such as std::vector is destroyed (its destructor is invoked) when the scope it lives in ends.
Code:
int main()
{
vector<int> a;
{
vector<int> b;
}// b is destroyed
}// a is destroyed
Cheers,
_
Re: best way to delete QVector<QVector3D>
You misunderstood me. vertices is a member variable of a class and not a local variable. I want to delete it after I construct it.
Re: best way to delete QVector<QVector3D>
Since it is a class member you can't delete it. To delete it you'd have to delete the whole class instance. You can only clear the vector which will remove its contents.