当vector中存储的类型为指针时,vector.clear()的执行结果

#include <vector>
#include <iostream>
using namespace std;
class A {
	private :
		int nId;
	public:
	A(int n)	{	nId = n; 
	cout << nId << " contructor" << endl;  }
	~A( ) //没有被调用
	 {cout << nId << " destructor" << endl; }
};
int main()  {
	vector<A*> vp;
	vp.push_back(new A(1));
	vp.push_back(new A(2));
	vp.clear(); //不调用~A()	
    A a(4);	
	return 0;
}

上述程序的输出为:

1 constructor

2 constructor

4 constructor

4 destructor


4 constructor之前没有输出1 destructor和2 destructor,说明析构函数~A()没有被调用

vector.clear()做的唯一的事情就是调用vector.tcc中的erase函数:

template<typename _Tp, typename _Alloc>
    typename vector<_Tp,_Alloc>::iterator
    vector<_Tp,_Alloc>::
    erase(iterator __first, iterator __last)
    {
      iterator __i(copy(__last, end(), __first));
      std::_Destroy(__i, end());//关键!!
      this->_M_impl._M_finish = this->_M_impl._M_finish - (__last - __first);
      return __first;
    }


erase会调用stl_construct.h中的_destroy函数,以下是源代码:

/**
   * @if maint
   * Destroy the object pointed to by a pointer type.
   * @endif
   */
  template<typename _Tp>
    inline void
    _Destroy(_Tp* __pointer)
    { __pointer->~_Tp(); }

  /**
   * @if maint
   * Destroy a range of objects with nontrivial destructors.
   *
   * This is a helper function used only by _Destroy().
   * @endif
   */
  template<typename _ForwardIterator>
    inline void
    __destroy_aux(_ForwardIterator __first, _ForwardIterator __last,
		  __false_type)
    { for ( ; __first != __last; ++__first) std::_Destroy(&*__first); }

  /**
   * @if maint
   * Destroy a range of objects with trivial destructors.  Since the destructors
   * are trivial, there's nothing to do and hopefully this function will be
   * entirely optimized away.
   *
   * This is a helper function used only by _Destroy().
   * @endif
   */
  template<typename _ForwardIterator>
    inline void
    __destroy_aux(_ForwardIterator, _ForwardIterator, __true_type)
    { }

  /**
   * @if maint
   * Destroy a range of objects.  If the value_type of the object has
   * a trivial destructor, the compiler should optimize all of this
   * away, otherwise the objects' destructors must be invoked.
   * @endif
   */
  template<typename _ForwardIterator>
    inline void
    _Destroy(_ForwardIterator __first, _ForwardIterator __last)
    {
      typedef typename iterator_traits<_ForwardIterator>::value_type
                       _Value_type;
      typedef typename __type_traits<_Value_type>::has_trivial_destructor
                       _Has_trivial_destructor;

      std::__destroy_aux(__first, __last, _Has_trivial_destructor());
    }
} // namespace std


当vector中存储的是对象时,调用第一个_destroy函数,对象的析构函数被调用。

但当vector中存储的是指向对象的指针时,调用的是第3,4个_destroy函数,其实什么也没干!

当然size之类的还是会被设为0的。。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值