C++ Class Utilizing the Rule of Five
The Rule of Five in C++ states that if a class requires a user-defined destructor, copy constructor, copy assignment operator, move constructor, or move assignment operator, it likely needs all five. Here's a complete example demonstrating this concept:
#include <iostream>
#include <cstring> // for strcpy and strlen
class RuleOfFiveExample {
private:
char* data;
size_t size;
public:
// Constructor
explicit RuleOfFiveExample(const char* str = "") {
size = strlen(str);
data = new char[size + 1];
strcpy(data, str);
std::cout << "Constructor called for: " << data << std::endl;
}
// 1. Destructor (Rule of Three)
~RuleOfFiveExample() {
std::cout << "Destructor called for: " << data << std::endl;
delete[] data;
}
// 2. Copy Constructor (Rule of Three)
RuleOfFiveExample(const RuleOfFiveExample& other) {
size = other.size;
data = new char[size + 1];
strcpy(data, other.data);
std::cout << "Copy constructor called for: " << data << std::endl;
}
// 3. Copy Assignment Operator (Rule of Three)
RuleOfFiveExample& operator=(const RuleOfFiveExample& other) {
if (this != &other) { // protect against self-assignment
delete[] data; // free existing resource
size = other.size;
data = new char[size + 1];
strcpy(data, other.data);
}
std::cout << "Copy assignment called for: " << data << std::endl;
return *this;
}
// 4. Move Constructor (Rule of Five)
RuleOfFiveExample(RuleOfFiveExample&& other) noexcept {
data = other.data;
size = other.size;
// Leave the source object in a valid but empty state
other.data = nullptr;
other.size = 0;
std::cout << "Move constructor called for: " << data << std::endl;
}
// 5. Move Assignment Operator (Rule of Five)
RuleOfFiveExample& operator=(RuleOfFiveExample&& other) noexcept {
if (this != &other) { // protect against self-assignment
delete[] data; // free existing resource
// Transfer ownership
data = other.data;
size = other.size;
// Leave the source object in a valid but empty state
other.data = nullptr;
other.size = 0;
}
std::cout << "Move assignment called for: " << data << std::endl;
return *this;
}
// Utility function to display the data
void display() const {
std::cout << "Data: " << data << ", Size: " << size << std::endl;
}
};
// Helper function to demonstrate move semantics
RuleOfFiveExample createAndReturn(const char* str) {
RuleOfFiveExample temp(str);
return temp; // This will use the move constructor
}
int main() {
// Constructor
RuleOfFiveExample original("Hello, Rule of Five!");
// Copy constructor
RuleOfFiveExample copyConstructed(original);
// Copy assignment
RuleOfFiveExample copyAssigned;
copyAssigned = original;
// Move constructor
RuleOfFiveExample moved = createAndReturn("Temporary object");
// Move assignment
RuleOfFiveExample moveAssigned;
moveAssigned = createAndReturn("Another temporary");
// Display all objects
std::cout << "\nDisplaying all objects:\n";
original.display();
copyConstructed.display();
copyAssigned.display();
moved.display();
moveAssigned.display();
return 0;
}
Key Points:
- Destructor: Releases the dynamically allocated memory
- Copy Constructor: Creates a deep copy of the object
- Copy Assignment Operator: Handles assignment between existing objects with deep copy
- Move Constructor: Transfers resources from a temporary object (rvalue)
- Move Assignment Operator: Transfers resources from a temporary object to an existing object
The noexcept specifier is used for move operations because they shouldn't throw exceptions (they just transfer resources).
This example demonstrates proper resource management in C++ when dealing with dynamically allocated memory, following the Rule of Five to prevent memory leaks and ensure correct object copying and moving.