How to deallocate object pointer in vector?

后端 未结 7 1400
野趣味
野趣味 2021-02-06 12:30

I have ;

class object {
                    // any private datas token in heap area 
             public : 
                   ~object () ; 

};
<
7条回答
  •  不思量自难忘°
    2021-02-06 13:17

    You put in the destructor whatever your class needs to free its members.

    As for the vector, it contains pointers, not object instances. As such, calling erase() will only remove pointers from the vector, but will not free the objects that the pointers are pointing at. You have to free the objects separately, eg:

    std::vector tmp;
    tmp.push_back(new object);
    ...
    std::vector::iterator iter = ...;
    delete *iter;
    tmp.erase(iter);
    

提交回复
热议问题