Rule of 5 — Writing a String Class
🔑 Rule of 5: own a raw resource → define dtor + copy ctor + copy assign + move ctor + move assign. Copy = deep (own buffer); move = steal pointer + null source + noexcept; copy-and-swap operator=(by value) is self-assign + exception safe and covers move. Default shallow copy = double free. Prefer Rule of 0: hold std::string and write none.
1class String {2 char* data_; // owned heap buffer (null-terminated)3 size_t size_;4 5public:6 // 1. Constructor — ACQUIRE the resource7 String(const char* s = "") : size_(std::strlen(s)) {8 data_ = new char[size_ + 1];9 std::memcpy(data_, s, size_ + 1);10 }11 // 2. Destructor — RELEASE the resource12 ~String() {13 delete[] data_;14 }15 // 3. Copy constructor — DEEP copy (own buffer)16 String(const String& o) : size_(o.size_) {17 data_ = new char[size_ + 1];18 std::memcpy(data_, o.data_, size_ + 1);19 }20 // 4. Copy assignment — copy-and-swap (self-assign + exception safe)21 String& operator=(String o) { // by value = the copy22 swap(*this, o);23 return *this;24 }25 // 5. Move constructor — STEAL the buffer, noexcept26 String(String&& o) noexcept : data_(o.data_), size_(o.size_) {27 o.data_ = nullptr;28 o.size_ = 0; // leave o destructible29 }30 // move assignment is covered by operator=(String o) above (by value)31 friend void swap(String& a, String& b) noexcept {32 std::swap(a.data_, b.data_);33 std::swap(a.size_, b.size_);34 }35};Why Rule of 5 — a class that owns a raw resource
String owns a heap buffer (char* data_). The moment a class manages a raw resource (heap memory, file handle, socket, mutex), the compiler-generated special members are WRONG: the default copy does a shallow pointer copy, so two objects free the same buffer → double-free. The Rule of 5 says: if you write any one of {destructor, copy ctor, copy assign, move ctor, move assign}, you almost always need all five. Define them so each object owns exactly one buffer.
Tap ▶ to play · tap the dots or Next → to stepPlays automatically · Space to play/pause · ← / → to step · controls are above the diagram