创建数组时,析构函数被调用太多次

时间:2020-03-30 18:28:42

标签: c++ oop destructor

我是编程和学习课程的新手。我有一个Product类,正在尝试创建一个产品数组,但是我的析构函数被两个对象调用了三次,并触发了一个断点。 这是一些代码:

`

    class Product {
    private:
        char* name;
        int price;

    public:
        Product();
        Product(const char*, int);
        ~Product();

    };
    `

        Product::Product(const char* name, int price) {
        this->name = new char[strlen(name) + 1];
        strcpy(this->name, name);

        this->price = price;
    }



    Product::~Product() {
    delete[] this->name;
}

    int main() {

    Product redPaint("Red Paint", 25);
    Product bluePaint("Blue Paint", 26);
    Product paint[2] = { redPaint, bluePaint};
}

0 个答案:

没有答案