C++ C++;字符串数组析构函数

C++ C++;字符串数组析构函数,c++,arrays,string,oop,destructor,C++,Arrays,String,Oop,Destructor,我有一个带有字符串指针和大小整数的简单类。它的构造函数将大小设置为0,并将指针设置为新字符串[0];当我试图用delete[]破坏字符串*时,程序崩溃 #include<string> using std::string; class Strarr { private: string *items; int size; public: Strarr() { items = new string[0]; size = 0;

我有一个带有字符串指针和大小整数的简单类。它的构造函数将大小设置为0,并将指针设置为新字符串[0];当我试图用delete[]破坏字符串*时,程序崩溃

#include<string>
using std::string;
class Strarr {
private:
    string *items;
    int size;
public:
    Strarr() {
        items = new string[0];
        size = 0;
    }
    ~Strarr() {
        delete[] items;
    }
};
int main() {
    Strarr test;
    test.~Strarr();
}
#包括
使用std::string;
Strarr类{
私人:
字符串*项;
整数大小;
公众:
斯特拉尔(){
items=新字符串[0];
尺寸=0;
}
~Strarr(){
删除[]项;
}
};
int main(){
斯特拉尔试验;
测试。~Strarr();
}

有人能解释一下这里发生了什么吗?

让我给你看一个我写的程序:-

#include <iostream>
using namespace std;
class Some_Class
{
    public:
    Some_Class()
    {
        cout<<"constructing\n";
    }
    ~Some_Class()
    {
        cout<<"destroying\n";
    }
};
int main()
{
    cout<<"executing...\n";
    Some_Class *p=new Some_Class[0];
    delete[] p;
    cout<<"executed\n";
    return 0;
}
/* OUTPUT:-
executing...
executed
*/
#包括
使用名称空间std;
上课
{
公众:
某个_类()
{

请告诉我们是谁教你应该手动调用析构函数的。然后我可以给一个能“修复”的人打电话事情。再想想这个循环,记住
size
0
。如果
size
是真实代码中的其他东西,那么当你试图
删除
你没有
的东西时,你会有未定义的行为。你知道,我不会手动调用析构函数。我这么做只是为了说明发生了什么。你知道吗当
main
函数返回时,变量
test
超出范围时,是否仍将调用析构函数?顺便说一句:您不会“使用delete[]销毁字符串*”。指针本身通过使用
delete[]保持不变
。相反,是它指向的字符串被破坏了。好的,我想我知道了。问题不在于删除字符串数组,而是删除对象。是的,如果您觉得这个答案令人满意,请在上面打绿色勾