Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.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++ vector正在调用析构函数,甚至在推回的时候。。。为什么?_C++_Vector - Fatal编程技术网

C++ vector正在调用析构函数,甚至在推回的时候。。。为什么?

C++ vector正在调用析构函数,甚至在推回的时候。。。为什么?,c++,vector,C++,Vector,我注意到这段代码中有些东西。即使在向量中推回元素(参见函数passtoit)。正在调用struct test的析构函数。谁能给我解释一下吗。。为什么会有这种行为 当我声明静态对象的std::vector并让它运行时,它会给我带来堆损坏问题;当我声明测试为测试指针(*)的std::vector,并删除该指针(如注释代码所示),它工作得很好。请解释一下。这对我很有帮助。为了让stackworkflow明白这是一个有效的问题,我不知道还要在这里写些什么 #include "stdafx.h"

我注意到这段代码中有些东西。即使在向量中推回元素(参见函数passtoit)。正在调用struct test的析构函数。谁能给我解释一下吗。。为什么会有这种行为

当我声明静态对象的
std::vector
并让它运行时,它会给我带来堆损坏问题;当我声明测试为测试指针(*)的
std::vector
,并删除该指针(如注释代码所示),它工作得很好。请解释一下。这对我很有帮助。为了让stackworkflow明白这是一个有效的问题,我不知道还要在这里写些什么

    #include "stdafx.h"
    #include <vector>
    #include <iostream>
    #include <vld.h>
    using namespace std;

    class declaration
    class testHelper
    {
    public:
        testHelper(int passed):m(passed){}

        int m;

        ~testHelper(){cout<<"deleting as thought";}
    };
     declaration of structure 
    struct test
    {
       // constructor
        test(string name,testHelper* help):name(name),dummy(help){}

        string name;

        testHelper *dummy;
    // destructor
        ~test()
        {
            cout<<"deleting dummy";

            if(dummy!=NULL)

            {
                delete dummy;

                dummy =NULL;
            }
        }
    };

    function to pass 
    int passtoit()
    {
        std::vector<test> x;
        // push back on the vector
        x.push_back(test("prakash",(new testHelper(10))));

        //for(std::vector<test>::iterator i =x.begin();i!=x.end();++i)

        //{

        //    delete *i;

        //}

        return 0;
    }
      main function
    int _tmain()
    {
        // calling the function
        passtoit();

        return 0;

    }
#包括“stdafx.h”
#包括
#包括
#包括
使用名称空间std;
类声明
类testHelper
{
公众:
testHelper(int-passed):m(passed){}
int m;

~testHelper(){cout使用
std::vector
时,元素被复制到向量中

x.push_back(test("prakash",(new testHelper(10))));

您正在创建一个实例,该实例被复制,然后立即被销毁。

当我们使用
push\u back
将元素插入向量时,将调用复制构造函数,然后为参数对象创建一个临时对象,然后将其插入向量。然后销毁/删除临时对象


若要检查它的实际工作方式,请尝试重写类的私有范围中的复制构造函数。
push_-back
函数将不会被调用,您将得到编译器错误。

首先,当向量被构造时,它将销毁所有元素

template<class _Ty> inline
    void _Destroy(_Ty _FARQ *_Ptr)
    {   // destroy object at _Ptr
    _DESTRUCTOR(_Ty, _Ptr);
    }
#define _DESTRUCTOR(ty, ptr)    (ptr)->~ty()
test& operator=(const test& t)
    {
        if(this == &t)
            return *this;
        name = t.name;
        dummy = new testHelper(*t.dummy);
        return *this;
    }