Module 16: C++ Copy and Move Semantics
- First read this page, then start the module with the GitHub classroom link below.
- Github Classroom Link: https://classroom.github.com/a/M4lxx4P7
Learning Objectives:
- Understand the difference between copy and move semantics in C++.
- Learn how to define and use copy constructors, copy assignment operators, move constructors, and move assignment operators.
- Understand the impact of these semantics on performance and memory management.
Exercise 1: Implementing Copy Constructor
Learn how copy constructors work and when they are invoked.
- In the
exercise1
folder of your GitHub repository, edit the fileBox.cpp
and implement a copy constructor in the classBox
with:- A copy constructor that performs a deep copy.
- Review the code in the file
main.cpp
. - Run make.
- Execute the program
main
. - Copy the output into the
README.md
file and give a brief explanation as to what happened.
Exercise 2: Copy Assignment Operator
Understand how to implement the copy assignment operator.
- In the
exercise2
folder of your GitHub repository, edit the fileBox.cpp
and implement the copy assignment operator for theBox
class:- Ensure that existing resources are cleaned up before performing a deep copy.
- Handle self-assignment correctly.
- Review the code in the file
main.cpp
. - Run make.
- Execute the program
main
. - Copy the output into the
README.md
file and give a brief explanation as to what happened.
Exercise 3: Understanding Move Constructor
Explore how move constructors allow efficient resource transfer.
- In the
exercise3
folder of your GitHub repository, edit the fileBox.cpp
and implement a move constructor to theBox
class:- Transfer ownership of the dynamically allocated array from the source object to the target object.
- Set the source object’s pointer to
nullptr
.
- Review the code in the file
main.cpp
. - Run make.
- Execute the program
main
. - Copy the output into the
README.md
file and give a brief explanation as to what happened.
Exercise 4: Move Assignment Operator
Explore the move assignment operator and resource transfer.
- In the
exercise4
folder of your GitHub repository, edit the fileBox.cpp
and implement a move assignment operator to theBox
class:- Release any existing resources of the target object.
- Transfer ownership of resources from the source object.
- Leave the source object in a valid state.
- Review the code in the file
main.cpp
. - Run make.
- Execute the program
main
. - Copy the output into the
README.md
file and give a brief explanation as to what happened.