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
}
};
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?