我需要在析构函数中清理双端队列中的对象吗?
我的双端队列包含对象,而不是指针。
例如:
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);
我知道它是否是指针,我必须在析构函数中清理它。
我的问题是:如果只是对象,我是否真的不必像这样清理 MyObject:
for ( unsigned int i = 0 ; i < m_deq.size(); i++ )
{
delete &m_deq[i];
}
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];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,如果您只在其中存储值对象,则不必清除
std::deque
。std::deque
析构函数将为它包含的每个对象调用析构函数。No, you do not have to clear the
std::deque
if you only stored value objects in there. Thestd::deque
destructor will call the destructor for each object it contains.