C++C++11C++14C++17C++20Learn C++

What Is The Rule Of Six In Modern C++?

What Is The Rule Of Six In Modern C++

In C++, classes and structs are one of the most important parts of modern application development. In modern C++, there are some rules to support the principles of programming, one of which is the Rule of Six in C++ (also known as the Rule of Five, excluding the constructor). In this post, we explain the Rule of Six in C++ with examples.

C++ is an Object-Oriented Programming (OOP) language, and OOP is a way to integrate with objects which can contain data in the form (attributes or properties of objects), and code blocks in the form of procedures (methodsfunctions of objects). Most developers find that using OOP techniques help them to map real-world behavior and bring an organizational structure to data. These attributes and methods are variables and functions that belong to the class – part of the class’s code and they are generally referred to as class members.

First, let’s refresh our memory about the fact that Resource Acquisition Is Initialization (RAII) in OOP programming, and the Single Responsibility Principle and how that relates to the Rule of Zero in C++.

What is resource acquisition in C++?

The principle of Resource Acquisition Is Initialization (RAII) term used in several OOP programming languages, which relates to the ability to manage resources, such as memory, through the copy and move constructors, destruction, and assignment operators. RAII is about the declaration and use of destructors, copy-move operators, and memory management in these members and methods. These cause new rules in development.

What is the single responsibility principle in C++?

The Single Responsibility Principle (SRP) is a computer programming principle that states “A module should be responsible to one, and only one, actor.” This principle exposes a rule for the classes in C++, called Rule of Zero. Now, let’s see what the Rule of Zero in C++ is.

What is the rule of zero in C++?

The Rule of Zero means that, if all members have default member functions, no further work is needed. This is the simplest and cleanest semantics of programming. The compiler provides default implementations for all of the default member functions if there are no special member functions that are user-defined. You should prefer the case where no special member functions need to be defined

Here is more about Rule of Zero with C++ Examples,

What is the rule of three in C++?

The Rule of Three states that if you need to define a class that has any of the following special member functions a copy constructor, copy assignment operator, or destructor then usually you need to define all these three special member functions. So, these 3 special member functions below should be defined if you have at least one of them defined,

  • Copy constructor
  • Copy assignment operator
  • Destructor

Here is more about Rule of Three with C++ examples,

What is the rule of six in C++?

The Rule of Three is outdated after C++11. C++11 comes with two additional special members of move semantics: the move constructor and the move assignment operator. So, there is another rule, the Rule of Six.

The Rule of Six states that if you need to define any of the six special members below,

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

then you probably need to define or delete (or at least consider) all six of them.

This rule also known as “The Rule of Five“, because the default constructor is special, and, therefore, sometimes excluded. Note that, when you define copy / move constructor you need to define constructor too, so the constructor needs to be added.

If you have a compiler that is compatible with the C++11 standard or over then if you need to define any of the six (or five) special members as in examples below.

Is there a simple example of the rule of six in C++?

Note that, a simple empty C++ class is perfectly equivalent to default implementations (Rule of Six) in a class. A modern compiler is able to provide all these special member functions (default implementations). In example, this simple class below:

is exactly the same as this one in modern C++.

Is there an example of the rule of six with defined members in C++?

As you can see, in modern C++, these 5 special members are automatically generated as a default for each new class. If you need to define one of them, you should define all of these members as in this example below,

Is there a full example of how to use the rule of six in C++?

Here is a full example of the rule of six in modern C++.

As you see in modern C++ these 6 special members are automatically generated as a default in simple class, if you need to define one of them (excluding constructor) you should define all of them.

What Is The Rule Of Six In Modern C++ C++ Builder logo

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise version.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?