Login to manage your account

Please enter a valid email address.
Forgot Password?
Please enter a valid password.
OR

Don't have an account yet? Sign up

What is the Rule of Three, Five and Zero?

These rules describe which special member functions must be defined together.

The Rule of Three (C++98): if you need to write any one of the destructor, copy constructor, or copy assignment operator, you almost certainly need all three. The reason is that needing a custom destructor implies the class manages a resource, and the compiler-generated copy operations will copy the pointer rather than the resource — producing a double free.

The Rule of Five (C++11) adds the move constructor and move assignment operator. Declaring a destructor or copy operation suppresses the implicit move operations, so a class following only the Rule of Three silently loses move optimisation.

The Rule of Zero is the modern guidance and the one to lead with: design classes so you need none of them. If every member is a type that manages itself — std::string, std::vector, std::unique_ptr — the compiler-generated versions are all correct, and there is no code to get wrong.

Note: The practical consequence is a separation of duties. Write resource-managing wrapper classes rarely and carefully, and let everything else follow the Rule of Zero. A class that both manages a raw resource and holds business logic is where these bugs live.

All C++ interview questions

Login to manage your account

Please enter a valid email address.
Forgot Password?
Please enter a valid password.
OR

Don't have an account yet? Sign up as