What is Rule of Three in C++?
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 (methods, functions 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. Classes and structs are very useful in modern developing tools. There are some rules to support the principles of programming, one of which is the Rule of Three in C++. In this post, we explain the Rule of Three in C++ with examples. 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. 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 What is the Rule of Three in C++? If you are new to classes and don’t know about these three special member functions, here are 3 posts about: Copy Constructor, Copy Assignment Operator, and Destructor, How can we apply the Rule of Three in C++? Here is a simple example that follows the Rule Of Three, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 class Ta { public: Ta(Ta const& other) // copy constructor { } Ta& operator =(Ta const& other) // copy assignment operator { } ~Ta() // destructor { } }; Here you may have constructor, or other special or user defined members too. Is there a full example of the Rule of Three […]
