Difference Between Copy and Move Operators in C++20 and Deleting Them

90 Views Asked by At

So, I am just starting a video tutorial on creating a graphics engine in C++ using GLFW and Vulkan. I have already completed the LunarG tutorial, but the series I am watching goes into far more detail including how to effectively move functions into classes which I struggled with as I am only intermediate in C++, and I do not know every single operator or all the ins-and-outs of C++ yet.

The tutorial first recommends for a class to delete the copy constructor and assignment operator like:

EngineDevice(const EngineDevice&) = delete;
void operator=(const EngineDevice&) = delete;

I understand how this works since copying can cause a dangling pointer, but I am confused on exactly what the move operator does as the person then suggests doing:

EngineDevice(const EngineDevice&&) = delete;
EngineDevice& operator=(const EngineDevice&&) = delete;

What exactly can happen if the move operator is allowed or removed? Does this prevent moving the memory location of an object to a new pointer? I am not exactly sure what is happening here, but I am still fairly new to C++ operators and things like moving, copying, etc.

This might be a stupid question, but I am trying to learn, and having it explained to me is far easier than combing through C++ documentation as I can get quite confused. I appreciate any help given. I have searched StackOverflow already, but I could not find anything which explains exactly what I am wanting. I am sorry if I missed an older post.

I'm not sure what to add to this "What I've Tried" section, but I have tried learning the difference myself via the C++ documentation, but I got confused very quick. I understand what the copy constructor does, but I am very confused on the move operator.

If I understand the documentation correctly, moving is when you intend to delete the original object right after copying?

0

There are 0 best solutions below