Copy & Destructors
Alright, so let's say you've built a class that uses new to grab some memory from the heap. Everything works great... until you try to copy that object. And then your program crashes. But why? Well, here's the thing — when C++ copies an object for you by default, it just copies everything field by field. For simple stuff like int and double, that's totally fine. But for pointers? It copies the address, not the actual data. So now you've got two objects pointing to the exact same chunk of memory. When one gets destroyed and frees that memory, the other one is left holding a pointer to nothing. Boom — crash.
This week, we're going to fix that. You'll learn the difference between a shallow copy (the dangerous default that just copies pointer addresses) and a deep copy (where you actually allocate fresh memory and duplicate the real data). And then we'll talk about the famous Rule of Three — basically, if your class needs a custom destructor, copy constructor, or copy assignment operator, it almost certainly needs all three. Think of it like this: if your class is responsible for cleaning up after itself, it also needs to know how to safely clone itself. By the end of this week, you'll be writing classes that manage dynamic memory like a pro — no memory leaks, no mysterious crashes.