首页 > 技术文章 > 这种情况下析构函数会调用到吗?

cchtj8k091 2013-07-11 12:10 原文

经test,vector pop一个element时会调到该element的析构

#include "stdafx.h"

#include <iostream>
#include <vector>
using namespace std;

class BaseA
{
public:
  BaseA()
  {
    m_count = 1;
    cout<<"BaseA(),pointer=0x08lx"<<(long)this<<endl;
  }

  ~BaseA()
  {
    m_count = 0;
    cout<<"~BaseA(),pointer=0x08lx"<<(long)this<<endl;
  }

  BaseA(const BaseA& base)
  {
    m_count = base.m_count;
    cout<<"BaseA() Copy,pointer=0x08lx"<<(long)this<<endl;
  }

private: 
  int m_count;
};

std::vector<BaseA> vec;
int main()
{
  {
  vec.push_back(BaseA());
  vec.pop_back();
  }

  system("pause");

  return 0;
}

 

 

推荐阅读