PDA

View Full Version : best way to delete QVector<QVector3D>



saman_artorious
3rd July 2013, 13:32
What is the best way to delete

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?

wysota
3rd July 2013, 13:41
Don't do anything. Let it go out of scope and all will be done for you.

saman_artorious
3rd July 2013, 13:44
but what if it doesn't go out of scope. I need to delete after right after drawing.


qDeleteAll(vertices.begin(), vertices.end());

gives the following error:

error: type 'class QVector3D' argument given to 'delete', expected pointer



template <typename ForwardIterator>
Q_OUTOFLINE_TEMPLATE void qDeleteAll(ForwardIterator begin, ForwardIterator end)
{
while (begin != end) {
delete *begin;
++begin;
}
}

wysota
3rd July 2013, 13:46
but what if it doesn't go out of scope. I need to delete after right after drawing.
Clear the vector.


vertices.clear();

saman_artorious
3rd July 2013, 13:51
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.

wysota
3rd July 2013, 14:19
What will be overloaded? Overloaded with what?

saman_artorious
3rd July 2013, 15:06
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.

wysota
3rd July 2013, 16:45
I'm sorry, I don't understand what you mean.

^NyAw^
3rd July 2013, 17:22
Hi,

What wysota want to tell you is that:


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:



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
}

anda_skoa
3rd July 2013, 17:29
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.



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.


int main()
{
vector<int> a;

{
vector<int> b;

}// b is destroyed

}// a is destroyed


Cheers,
_

saman_artorious
4th July 2013, 08:47
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.

wysota
4th July 2013, 09:11
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.