Explain the 'Rule of Three', 'Rule of Five', and 'Rule of Zero'.
Last updated
Rule of Three: if a class defines its destructor, copy constructor, or copy assignment, it needs all three. Missing one causes leaks, double-frees, or shallow copies.
Rule of Five (C++11) adds move constructor and move assignment. Declaring a destructor or copy op suppresses implicit moves, so define them explicitly (
= default/= deletehelp).Rule of Zero: preferably define none by holding resources in RAII members (
std::unique_ptr,std::vector,std::string) so compiler-generated specials are correct.