Rule of three (C++ programming)

From Wikipedia, the free encyclopedia

The rule of three (also known as the Law of The Big Three or The Big Three) is a rule of thumb in C++ that claims that if a class or struct has one of the following it should probably have all three.

These three methods are special member functions which are created by the compiler automatically if they are not defined by the programmer. If one of these had to be explicitly defined by the programmer it means that the default compiler generated version does not fit the needs of the class in one case and it will probably not fit in the other cases either.

An amendment to this rule is that if Resource Acquisition Is Initialization (RAII) is used the destructor may be left undefined (also known as The Law of The Big Two[1]).

See an example of why an explicit copy constructor is needed in some cases (the same holds true for the assignment operator).

[edit] References

  1. ^ http://www.artima.com/cppsource/bigtwo.html