Chapter 14: Problem 24
Explain why memberwise assignment can cause problems with a class that contains a pointer member.
Short Answer
Expert verified
Question: Explain why memberwise assignment can cause problems with a class that contains a pointer member.
Answer: Memberwise assignment can cause problems with a class containing a pointer member because the default assignment operation provided by the compiler only copies the memory address, not the actual memory block. This leads to both the source and target objects pointing to the same memory block, which can cause issues like double deletion, unintentional mutation, and incorrect behavior. To avoid these problems, a custom copy assignment operator should be defined to ensure the actual memory block is copied, allowing each object to have its own separate memory.