每次调用析构函数时,我都会收到一条奇怪的消息。由于我的私有变量之一是动态分配的数组(int *member;
),我这样写析构函数:
ClassSet::~ClassSet(){delete []member;}
每次ClassSet
调用析构函数时,我都会收到一条错误消息:
Windows 已在 Hw1.exe 中触发断点。
这可能是由于堆损坏,这表明 Hw1.exe 或其已加载的任何 DLL 中存在错误。
这也可能是由于用户在 Hw1.exe 具有焦点时按 F12。
全班:
class ClassSet
{
public:
ClassSet(int n = DEFAULT_MAX_ITEMS);
ClassSet(const ClassSet& other);
ClassSet &operator=(const ClassSet& other);
~ClassSet();
private:
int size;
int *member;
};
ClassSet::ClassSet(int n){
size = n;
member = new int[n];
}
ClassSet::ClassSet(const ClassSet& other){
int i = 0;
this->size = other.size;
member = new int [capacity];
while (i<size)
{
this->member[i] = other.member[i];
i++;
}
}
Multiset& Multiset::operator=(const Multiset &other)
{
if (&other == this){return *this;}
this->size = other.size;
int i = 0;
delete [] member;
member = new int[size];
while (i<other.size)
{
this->member[i] = other.member[i];
i++;
}
return *this;
}
知道这个析构函数有什么问题吗?