Skip to main content

rule of 3 5 6 in C++

 

Rule of 3 (C++98/03)

In C++98/03, the Rule of 3 states that if a class defines one of the following special member functions, it should likely define all three:

  1. Destructor: Cleans up any dynamically allocated memory or other resources.
  2. Copy Constructor: Creates a new object as a copy of an existing object.
  3. Copy Assignment Operator: Assigns the contents of one object to another.

Rule of 5 (C++11 and beyond)

With the introduction of C++11, the Rule of 5 builds upon the Rule of 3 and adds two more special member functions:

  1. Move Constructor: Transfers resources from one object to another without copying.
  2. Move Assignment Operator: Transfers resources from one object to another using move semantics rather than copying.

Rule of 6 (Extended rule, but not part of the C++ standard)

In some discussions, you may encounter references to the Rule of 6, which is an extension of the Rule of 5. It specifically addresses the issue of defining default constructor alongside the other special functions.

The Rule of 6 says that if you define any of the following special functions, you should define all six of them:

  1. Destructor
  2. Copy Constructor
  3. Copy Assignment Operator
  4. Move Constructor
  5. Move Assignment Operator
  6. Default Constructor

This rule helps to ensure that all necessary behaviors are correctly defined for managing resource ownership and memory management. In practice, it's more of a guideline to help developers avoid incomplete implementations when defining complex classes with custom resource management.

Comments

Popular posts from this blog

FIX protocol interview questions : Part 1 - Admin/Session

Well, I just felt a need that key FAQs in FIX be documented, so that can be used for a variety of purposes, primarily during interviews to gauge a candidate. I have tried to be correct to the max, but do not guarantee it. This is what is best to my knowledge, and its good to cross check things with the FIX site and documentation available there. 1. What is buy side and what is sell side ? Sell side is the one who earns brokerage. Buy side is the one who pays brokerage. buy side (institutions) sell side (brokers/dealers) 2. What is the latest FIX version in market now? Its FIX 5.0 SP2. Something called FIXT.1.1 is also coming. 3. What is FIXML? FIXML is a structured, validated Extensible Markup Language (XML) derived grammar that is encapsulated within the standard FIX message. This format leaves the FIX session handling intact and minimizes the impact to existing implementations. As an XML-derived language, FIXML messages can be validated by standard parsers and take advantage of a fle...

what is a core dump

 A core dump (or core file ) is a file that captures the memory contents of a running process at a particular point in time, typically when the process crashes or encounters a serious error, such as a segmentation fault or illegal instruction. What is a core dump? It is essentially a snapshot of a process’s memory, including the call stack, memory allocations, and the state of the program at the time of the crash. It helps developers analyze the cause of a crash by examining the state of the program when the error occurred. Why does a core dump occur? A core dump typically happens when a process encounters a fatal error that causes it to terminate unexpectedly. This might include: Segmentation faults (segfaults): Trying to access memory that the process is not allowed to. Illegal instructions: Executing invalid machine instructions. Memory access violations: Trying to access memory in a way that is not allowed (e.g., reading from or writing to protected memory). Un...

std::unordered_map problems

 The std::unordered_map in C++ is a powerful container that provides fast access to key-value pairs with average constant-time complexity ( O(1) ) for lookups, inserts, and deletions. However, like any data structure, it has its potential issues, trade-offs, and limitations. Here are some common problems and limitations associated with std::unordered_map : 1. Unpredictable Iteration Order Problem: The order of elements in an unordered_map is not guaranteed. It depends on the internal hash table implementation and the distribution of hash values. Impact: This makes std::unordered_map unsuitable for use cases where the order of elements matters, such as when you need to traverse the elements in a specific order. Example: cpp Copy std::unordered_map< int , std::string> map; map[ 1 ] = "one" ; map[ 2 ] = "two" ; map[ 3 ] = "three" ; // The order in which elements are iterated is not guaranteed for ( auto & entry : map) { std::cout ...