Jump to content

Rule of three (C++ programming)

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Lanzkron (talk | contribs) at 07:09, 2 January 2007 (Created page with '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 o...'). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

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.

  1. destructor
  2. copy constructor
  3. assignment operator

These three methods are created by the compiler automatically if they are not defined by the programmer and if the default compiler generated version does not fit the needs of the class in one case 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. [1]