Problem Statement
Explain the Rule of Three (and Rule of Five) in C++ and why it matters for memory management in classes.
Explanation
In C++ the Rule of Three states that if you define a custom destructor, copy constructor or copy assignment operator then you should probably define all three to manage resources safely. With move semantics (C++11) this extends to the Rule of Five (add move constructor and move assignment). Following these rules prevents shallow copies, double deletion, and resource leaks. Interviewers who ask this expect you to recognise how constructors, destructors and assignment interplay in safe class design.
Practice Sets
This question appears in the following practice sets: