There is a structure of the form:

struct MyStruct { qint32 field1; qint32 field2; QVector<qint16> field3; }; 

After using the object of this structure, you need to clear the QVector. How can this be done? Now only this solution comes to mind: declare the clear () method inside the structure, which will reset the structure fields and call the appropriate method to clear the vector:

 struct MyStruct { qint32 field1; qint32 field2; QVector<qint16> field3; void clear() { field1 = 0; field2 = 0; field3.clear(); } }; 
  • You can reset each field separately (the fields are public in the structures), or you can add a constructor that will reset the fields, and thus you can simply assign the newly created object from scratch to an already existing object. Clarify your question with explanations, because beyond the obvious answers the doubt creeps in that you ask exactly about them. - alexis031182
  • The structure object is used several times. Accordingly, after each use, you need to clear the vector, because with each new use it contains a different number of elements. - nulll

1 answer 1

If you store value types in your vector, then you don’t need to invent anything separately: the destructor of your structure will cause the vector destructor, which in turn will destroy the objects stored in it.