我不明白为什么在最后一行return *newP; 进入复制构造函数的方法会导致内存泄漏,因为我无法释放Poly
从复制构造函数创建的新对象?
const Poly Poly::operator * (const Poly &p1) const {
Poly* newP= new Poly;
delete newP->headList->mono;
newP->headList->mono=NULL;
delete newP->headList;
newP->headList=NULL;
newP->SetCount(0);
//two pointer to run on the list
Node* tmp,* tmp2;
tmp= this->headList;
tmp2=p1.headList;
Mono* mono= new Mono;
int n,d,p;
while (tmp != NULL)
{
while (tmp2!=NULL)
{
*mono=(*tmp->mono)*(*tmp2->mono);
n=mono->GetiNom();
d=mono->GetiDenom();
p=mono->GetPower();
newP->Insert(n,d,p);
tmp2=tmp2->next;
}
tmp=tmp->next;
tmp2=p1.headList;
}
delete mono;
return *newP;
}