What is The Rule of Three with reference to C++?

The Rule of three is a rule of thumb when using C++. This is kind of a good practice rule that says that If your class needs any of

  • a copy constructor,
  • an assignment operator,
  • or a destructor,

defined explicitly, then it is likely to need all three of them.

Why is this? Its because, if your class requires any of the above, it is managing dynamically allocated resources and would likely be needing the other to successfully achieve that. For example, if you require an assignment operator, you would be creating copies of objects currently being copied by reference, hence allocating resources. You'll need the copy constructor for copying and destructor for freeing up these resources.