How do I delete copy/move operations when needed in C++?

In C++, there are scenarios where you may want to prevent instances of a class from being copied or moved. This is often done to maintain the integrity of resources, like managing unique pointers or ensuring that a given instance has exclusive access to a resource. To delete copy and move operations, you can use the `delete` keyword in the class definition. Below are some examples to illustrate how to delete copy and move constructors and assignments in a class.

class MyClass { public: MyClass() = default; // Default constructor // Delete copy constructor and copy assignment operator MyClass(const MyClass&) = delete; MyClass& operator=(const MyClass&) = delete; // Delete move constructor and move assignment operator MyClass(MyClass&&) = delete; MyClass& operator=(MyClass&&) = delete; void doSomething() { // Implementation of the class functionality } };

C++ delete copy constructor move constructor prevent copying unique resource management