C++ destructor issue with std::vector of class objects

后端 未结 4 1621
感动是毒
感动是毒 2020-12-15 01:03

I am confused about how to use destructors when I have a std::vector of my class.

So if I create a simple class as follows:

class Test
{
private:
 i         


        
4条回答
  •  暖寄归人
    2020-12-15 01:46

    Without a copy-constructor, the vector will create a flat copy of your object. This leads to two objects of type Test referencing the same array big. The first instance deletes the array when it gets destroyed, and then the second instance try to dereference a deleted pointer, which is undefined behavior.

提交回复
热议问题