🚀
Copy-and-Swap Idiom (Crux)
Goal:
Safely define how objects are assigned (a = b) — especially when your class manages memory or resources.
🧠 How It Works
- Make a copy of the object (b).
- Swap its contents with a.
- The temporary copy (b) now holds old a data and is destroyed automatically.
That’s it!
💡 Why Use It?
- Cleaner code ✅
- Avoids resource leaks ✅
- Handles self-assignment (a = a) safely ✅
- Uses destructor to auto-clean old stuff ✅
🧪 Mini Example