Shallow copy works for most classes that do not have pointers. However, a class that contains pointers may require a deep copy operation. Here is an example:
From this definition, we know that setMessage allocates storage for the message so that an object of class Y has its own storage for the message.
On line 27, y2 is shallow-copied from y1. This means y2.pMessage points to the same storage as y1.pMessage, with the content of "abc".
On line 28, y2 calls its setMessage method. It first deletes the storage pointed to by pMessage, then reallocates for the new string. However, because both y1.pMessage and y2.pMessage point to the same allocated storage, y2.setMessage deallocates the storage for y1!
There is no symptom shown until y1 attempts to use its pMessage. On line 30, a run-time error is generated because y1.setMessage attempts to delete the storage pointed to by y1.pMessage, but that storage is deallocated already!