Well, you didn't overload the operator=
for Dynamic
, so the compiler implicitly defines one which would do bitwise copy. stable_sort()
in your library calls the operator=
, so tab
in two Dynamic
objects points to the same address, as a result, double delete on destruction. Overloading the operator=
would resolve the problem:
Dynamic& operator =(const Dynamic& d)
{
// or use the copy-and-swap idiom
if(this != &d)
{
delete [] tab;
n = d.getN();
tab = new int[n];
for (int i = 0; i<n; i++)
tab[i] = d.getTab()[i];
}
return *this;
}