Rule of three (C++ programming)

Rule of three (C++ programming)

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 defines one of the following it should probably explicitly define all threecite book
last = Stroustrup
first = Bjarne
authorlink = Bjarne Stroustrup
title = The C++ Programming Language
edition = 3
publisher = Addison-Wesley
date = 2000
pages = 1030
isbn = 978-0201700732
ref = stroustrup
pages = 283-4
] :

*destructor
*copy constructor
*copy assignment operator

These three methods are special member functions that are automatically created by the compiler if they are not explicitly declared by the programmer. If one of these had to be defined by the programmer, it means that the 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 [cite web
last = Karlsson
first = Bjorn
coauthors = Wilson, Matthew
title = The Law of the Big Two
work = The C++ Source
publisher = Artima
date = 2004-10-01
url = http://www.artima.com/cppsource/bigtwo.html
accessdate = 2008-01-22
] .

Because implicitly-generated constructors and assignment operators simply copy all class data members [cite book
title = The C++ Programming Language
ref = stroustrup
pages = 271
] , defining explicit copy constructors and copy assignment operators is needed in cases where the class encapsulates complex data structures or could hold exclusive access to resources.

References


Wikimedia Foundation. 2010.

Игры ⚽ Поможем сделать НИР

Look at other dictionaries:

  • Rule of Three — may refer to: *Rule of three (medicine), judgement of likelihood of harm in drug trials. *Rule of three (Wiccan), a tenet of Wicca *Rule of three (mathematics), a computation method in mathematics *Rule of three (writing), a principle of writing… …   Wikipedia

  • Rule of three (programming) — Rule of three is a code Refactoring rule of thumb to decide when a replicated piece of code should be replaced by a new procedure. It states that you are allowed to copy and paste the code once, but that when the same code is replicated three… …   Wikipedia

  • Big Three — The term Big Three may refer to:Government and education* Big Three (colleges), Harvard, Yale, and Princeton * The Big Three Allied leaders of World War I, the dominant diplomatic figures at the Paris Peace Conference of 1919, Lloyd George,… …   Wikipedia

  • Three Laws of Robotics — In science fiction, the Three Laws of Robotics are a set of three rules written by Isaac Asimov, which almost all positronic robots appearing in his fiction must obey. Introduced in his 1942 short story Runaround , although foreshadowed in a few… …   Wikipedia

  • Constructor (object-oriented programming) — Programming language comparisons General comparison Basic syntax Basic instructions Arrays Associative arrays String operations …   Wikipedia

  • Abstraction principle (programming) — In software engineering and programming language theory, the abstraction principle (or the principle of abstraction) is a basic dictum that aims to reduce duplication of information in a program (usually with emphasis on code duplication)… …   Wikipedia

  • References to the Three Laws of Robotics — References to Isaac Asimov s Three Laws of Robotics have appeared in a wide variety of circumstances. In some cases, other authors have explored the Laws in a serious fashion. Jack Williamson wrote a disturbing short story called With Folded… …   Wikipedia

  • List of programming languages by category — Programming language lists Alphabetical Categorical Chronological Generational This is a list of programming languages grouped by category. Some languages are listed in multiple categories. Contents …   Wikipedia

  • Constraint logic programming — Programming paradigms Agent oriented Automata based Component based Flow based Pipelined Concatenative Concurrent computing …   Wikipedia

  • Dataflow programming — Programming paradigms Agent oriented Automata based Component based Flow based Pipelined Concatenative Concurrent computing …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”