There are two ways to fix the excessive-reclamation problem introduced by the destructor. One way is to define your own copy constructor such that the entire serial number is copied, rather than only the pointer. Then, when the destructor reclaims the copy's car-number memory chunk, no harm is done to the original car-number memory chunk:
Memory reserved for Memory reserved for r, railroad_car argument the railroad_car parameter | | v v *-----* Copy *-----* | | -------------> | | |-----| |-----| | | | | |-----| |-----| | | ----* | | ----* |-----| | *-----* | | | | | | | | v v *---*---*---*- *---*---*---*- | | | | | | | | *---*---*---*- *---*---*---*-