Posts

Showing posts with the label copy assignment operator

CPP Crash Course: Move Assignment notes.

Today I added the final implementation of the Move Assignment Operator to the example I am working on from CPP Crash Course. It is still a work in progress, and I have some more notes to add, but it works and I have included the valgrind output in a separate text file. More explanation will be forthcoming, but I will briefly list some concepts and ideas that are crucial for understanding how this works: rvalue / lvalue references The default behaviour of copy and move assignment operators. Constructors and Class Objects. Without understanding these concepts, you will find it difficult to understand how the copy and move assignment operators work. Without a lot of practise and experience, is easy to forget crucial details in how these ideas are implemented. So it is important whether you are learning for the first time, or reviewing the knowledge, that you check the reference documents and ensure that you really know what is going on here. Links to the files: https://github.com/pereirad...

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...