
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
What is Rule of Five in C++11?
The rule of five is applied in C++ for resource management. Resource management frees the client from having to worry about the lifetime of the managed object, potentially eliminating memory leaks and other problems in the C++ code. But this management comes at a cost. The Rule of The Big Five states that if you have to write one of the following functions then you have to have a policy for all of them. If we have an Object Foo then we can have a FooManager that handles the resource Foo. When implementing FooManager, you'll likely all need the following functions to be implemented −
destructor − When this manager goes out of scope it should free all the resources it was managing.
assignment operator − If you do not provide one the compiler creates a default assignment operator. The default assignment operation is a member-wise copy function and does a shallow copy and not a deep copy. This could cause problems like memory leaks, wrong assignement.
copy constructor − The compiler-supplied copy constructor does a member-wise copy of all the FooManager's attributes. This poses the same problems as the assignement operator.
move constructor − Copying objects can be expensive as it involves creating, copying and then destroying temporary objects. C++11 introduced the concept of the r-value reference. An r-value reference can be explicitly bound to an r-value. An r-value is an unnamed object. A temporary object, in other words. This r-value reference can be used in a constructor to create a reference to the r-value passed to it.
move assignment operator − It is useful to only have one resource at a time. This resource's ownership can be transferred from one manager to another. In such cases you can provide a move assignment operator.
This is a great resource to learn about the rule of five − https://www.feabhas.com/sites/default/files/2016-06/Rule%20of%20the%20Big%20Five.pdf.
- Related Articles
- Rule of Three vs Rule of Five in C++?
- What is the divisibility rule of 11?
- Explain the Divisibility rule of 11.
- Check the divisibility rule of 11 for 1.10000001.
- What is The Rule of Three with reference to C++?
- What is RHS rule?
- Rule Of Three in C++
- What is a lambda expression in C++11?
- What is the working of Association Rule?
- What is Association rule learning?
- What is ASA congruence rule ?
- C++ Rule Of Three.
- What is horizontal rule in HTML Page?
- Find the square root of 11 correct to five decimal places.
- What is rule for perimeter of rectangle and square?
