Search code examples
c++deque

Do I need to clean the objects in the deque in the destructor?


my deque contains object, NOT pointer.

such as:

class MyObject                // no pointer inside the object, all simple type/prmitive
{
  string name;
  string value;
}

MyObject object1, object2;
m_deq.push_back(object1); 
m_deq.push_back(object2); 

I know if it's pointer, I have to clean it in desconstructor.

My question is: if just object, Is it true that I don't have to clean MyObject like:

for ( unsigned int i = 0 ; i < m_deq.size(); i++ )
{
    delete &m_deq[i];
}

Solution

  • No, you do not have to clear the std::deque if you only stored value objects in there. The std::deque destructor will call the destructor for each object it contains.