Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 复制构造函数和析构函数的无关调用_C++_Destructor_Copy Constructor - Fatal编程技术网

C++ 复制构造函数和析构函数的无关调用

C++ 复制构造函数和析构函数的无关调用,c++,destructor,copy-constructor,C++,Destructor,Copy Constructor,[后续行动] 为了清楚地看到发生了什么,我建议在输出中包含this指针,以确定哪个A正在调用该方法 A() {cout<<"A (" << this << ") Construction" <<endl;} A(A const& a){cout<<"A (" << &a << "->" << this << ") Copy

[后续行动]


为了清楚地看到发生了什么,我建议在输出中包含
this
指针,以确定哪个A正在调用该方法

     A()          {cout<<"A (" << this << ") Construction"     <<endl;}
     A(A const& a){cout<<"A (" << &a << "->" << this << ") Copy Construction"<<endl;}
    ~A()          {cout<<"A (" << this << ") Destruction"      <<endl;}
因此,流量可以解释为:

  • 此时将创建临时A(…cf)
  • 将临时A(…cf)复制到向量(…60)中
  • 临时A(…cf)被销毁
  • 将创建另一个临时(…ce)
  • 展开向量,并将该向量中的旧A(…60)复制到新位置(…70)
  • 另一个临时A(…ce)被复制到向量(…71)中
  • 所有不必要的(…60,…ce)副本现在都已销毁
  • 向量被破坏了,所以里面的A(…70,…71)也被破坏了
  • 如果你这样做,第五步就不复存在了

        vector<A> t;
        t.reserve(2); // <-- reserve space for 2 items.
        t.push_back(A());
        t.push_back(A());
    
         A()          {cout<<"A (" << this << ") Construction"     <<endl;}
         A(A const& a){cout<<"A (" << &a << "->" << this << ") Copy Construction"<<endl;}
        ~A()          {cout<<"A (" << this << ") Destruction"      <<endl;}
    
    A (0xbffff8cf) Construction
    A (0xbffff8cf->0x100160) Copy Construction
    A (0xbffff8cf) Destruction
    A (0xbffff8ce) Construction
    A (0x100160->0x100170) Copy Construction
    A (0xbffff8ce->0x100171) Copy Construction
    A (0x100160) Destruction
    A (0xbffff8ce) Destruction
    A (0x100170) Destruction
    A (0x100171) Destruction
    
        vector<A> t;
        t.reserve(2); // <-- reserve space for 2 items.
        t.push_back(A());
        t.push_back(A());
    
    A (0xbffff8cf) Construction
    A (0xbffff8cf->0x100160) Copy Construction
    A (0xbffff8cf) Destruction
    A (0xbffff8ce) Construction
    A (0xbffff8ce->0x100161) Copy Construction
    A (0xbffff8ce) Destruction
    A (0x100160) Destruction
    A (0x100161) Destruction