Here is the code for my destructor for the class MyClass

~MyClass() { for ( std::vector< MyStruct* >::const_iterator ci = elements.begin(); ci != clients.end(); ++ci ) { delete *ci; } clients.clear(); } 

I use const_iterator , everything compiles and works. Or maybe you need to use an iterator . Still, the const_iterator says that we will not change the object to which it points, and we delete it - is it essentially a change, or am I wrong? It is true that I use const_iterator

    1 answer 1

    That's right. Your vector stores pointers (not constant), so using a constant iterator you can change the objects to which these pointers refer, but you cannot change the pointer values ​​themselves:

      std::vector< MyStruct* >::const_iterator ci = elements.begin(); *ci = &otherMyStruct; // нельзя (*ci)->someNonconstMethod(); //можно 

    The delete directory does not change the pointer value, so the values ​​of the pointers will remain the same after the cycle, but they will point to the freed memory area.