|
|
|
|
coeff2
will delete coeff
if not NULL. So remove line 4.poly(int n, bool *a);
and poly2(int n, poly *a);
|
|
But I need to free the memory allocated to coeff2[i].coeff in the destructor of poly2, no? |
poly
does it already.for(i=0;i<degre2;i++) coeff2[i]=a[i];
coeff
.for(i=0;i<degre2;i++) coeff2[i]=a[i]; The assignment above is a problem. You're assigning (indirectly) the pointer to coeff. That could lead to both: double delete and memory leak |
If I use the first version of ~poly2(), it deletes coeff2, but does it call the destructor ~poly() as well? |
How can I tell? |
You mean I'm assigning a objet of class poly to coeff2[i]? |
Why could this lead to double delete and memory leak? |
coeff2[i]=a[i];
->
|
|
|
|
Make a cout or something in the destructor and test it with a reasonable number |
Well effectively you do (or better: the compiler does it for you): coeff2[i]=a[i];-> 1 2 coeff2[i].degre=a[i].degre; coeff2[i].coeff=a[i].coeff; // Here the pointer is copied not(!) the content |
|
|
I'm not sure but i saw this 1 2 3 ~poly(); and ~poly2(); is all you need. No { //delete [] blah;}. Just that. Try it |
|
|
They did complain about that. This is what i saw: 1 2 3 4 5 6 7 8 9 ~poly() { cout << "poly destroyed"; } and ~poly2() { cout << "poly2 destroyed"; } Did it work? |
is all you need. No { //delete [] blah;}. Just that. Try it |
Here's the = operator: |
But how can I tell if the default destructor frees the memory? |