Posts

Showing posts from March, 2025

Using the Copy Assignment Operator to copy objects that have raw pointers.

Listen to this article: https://youtu.be/MdGPC9C5QEI According to cplusplus.com: "A copy constructor is a special constructor for a class/struct that is used to make a copy of an existing instance." The Implicit Copy Constructor      If a copy constructor is not declared, the compiler will use a default one implicitly. The default constructor performs a member-wise copy of the source object. This means that each member is initialized using the corresponding member of the initializer.     Problem: Raw pointers.      If the object contains raw pointers, a member-wise copy may not be appropriate for use. This can cause problems with dangling pointers, double frees, and other undefined behaviour resulting from destructing objects.     Solution: Deep copy.      We explicitly define our own copy constructor when we want to perform a deep copy. That is, instead of copying the pointer, our copy constructor will copy what the po...