开发者

Clear ptr_array

开发者 https://www.devze.com 2023-02-18 22:42 出处:网络
I want to clear ptr_array boost::ptr_array<A, 10> a; ... a.clear();// missing How do I clean ptr c开发者_运维技巧ontainer?It is supposed to behave like an array and you can not clear an array

I want to clear ptr_array

boost::ptr_array<A, 10> a;
...
a.clear();  // missing

How do I clean ptr c开发者_运维技巧ontainer?


It is supposed to behave like an array and you can not clear an array in C++. Only thing you can do is to set the individual elements to NULL.


According to the class synopsis, calling a.release(); will do the trick, as the docs state that a postcondition of calling release is that "all pointers are null."

Indeed, a glance at the implementation verifies this, although it's less efficient than strictly possible since it involves an unused/wasted (for your purposes) heap allocation:

std::auto_ptr<this_type> release()
{
    std::auto_ptr<this_type> ptr( new this_type );
    this->swap( *ptr );
    return ptr;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消