![[Dept of Engineering]](http://www.eng.cam.ac.uk/images/house_style/engban-s.gif)
void routine (criminal a)
{
criminal b;
b=a;
...
}
The assignment b=a copies the bytes of a to b, so the fingerprint field of both objects will be the same, pointing to the same memory. At the end of this routine criminal's destructor will be called for b, which will delete the memory that b.fingerprint points to, which unfortunately is the same memory as a.fingerprint points to.
To cure this we need to redefine the assignment operator so that the new object has its own copy of the resources.
void criminal::operator=(criminal const &b)
{
name=b.name;
fingerprint_size=b.fingerprint_size;
delete fingerprint;
fingerprint = new char[fingerprint_size];
for (int i=0;i<fingerprint_size;i++)
fingerprint[i]=b.fingerprint[i];
}
Note that space used by an existing fingerprint is freed first before a
new fingerprint image is created - memory would be wasted otherwise.