c++ - 制作数组时调用了太多次析构函数
问题描述
我是编程和学习课程的新手。我有一个类 Product 并试图创建一个产品数组,但是我的析构函数为两个对象调用了 3 次,它触发了一个断点。这是一些代码:
`
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};
}
解决方案
推荐阅读
- csv - 使用 tf.data.experimental.make_csv_dataset 获取时间序列数据
- php - After user logging in UPDATE session id in database
- javascript - 如何使用 Tabulator 限制可编辑表中输入的最大长度?
- python - SQL炼金术 | 多个一对一和一对多关系
- javascript - 当我想打印数组中的所有项目时,javascript getItem 打印最后一个数组
- php - 如何在 php/html 中获取当前和下个月的日期?
- pandas - 使用 pandas 合并合并 3 个数据框
- json - 有没有办法改进 Grails 4 JSON 转换器处理 LocalDate 对象的方式?
- docker - 标记 docker 多阶段构建的所有阶段
- r - 在 bar/col 图中堆叠多列 df 并创建 %