How to Use Deleted Functions to Prevent Object Copying in C++? Last Updated : 12 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In C++, the class always has a copy constructor and assignment operator, whether it is default or user-defined which allows the program to create copies of the objects of that class. But sometimes, we need to create a class whose object should not be copied. In this article, we will learn how to use the deleted function concept to prevent object copying in C++. Prevent Object Copying using Deleted Functions We can prevent the copying of an object belonging to a particular class by deleting the constructor and copy assignment operator using the delete keyword. To delete a function, we can use the following syntax: function_declaration {} = delete;C++ Program to Prevent Object Copying Using Deleted Functions C++ // C++ program demonstrating a class that cannot be copied #include <iostream> using namespace std; class NonCopyables { public: NonCopyables() = default; // Delete copy constructor NonCopyables(const NonCopyables&) = delete; // Delete assignment operator NonCopyables& operator=(const NonCopyables&) = delete; // Member function to display a message void showMessage() const { cout << "This object cannot be copied." << endl; } }; int main() { // Create an instance of the NonCopyables class NonCopyables obj1; // Call the showMessage method to display a message NonCopyables obj2 = obj1; return 0; } Output main.cpp: In function ‘int main()’: main.cpp:26:25: error: use of deleted function ‘NonCopyables::NonCopyables(const NonCopyables&)’ 26 | NonCopyables obj2 = obj1; | ^~~~ main.cpp:10:5: note: declared here 10 | NonCopyables(const NonCopyables&) = delete; | ^~~~~~~~~~~~ Explanation We define a class NonCopyables with the default constructor and delete the copy constructor and assignment operator using the delete keyword. In the main() function, we attempt to create a copy of obj1 which results in a compilation error due to a deleted copy constructor. Then we tried to create a copy of the obj1 but we got the error instead. Comment More infoAdvertise with us Next Article How to Use Deleted Functions to Prevent Object Copying in C++? V v29581x10 Follow Improve Article Tags : C++ Programs C++ CPP-Functions cpp-constructor CPP-OOPs CPP Examples +2 More Practice Tags : CPP Similar Reads How to Create a Pure Virtual Function in C++? In C++, pure virtual functions are those functions that are not implemented in the base class. They are instead implemented in the derived classes if necessary. In this article, we will discuss how to create a pure virtual function in a class in C++. How to Create a Pure Virtual Function in C++? To 2 min read How to Return a Pointer from a Function in C++? In C++, we can return a pointer from a function which is useful when we want to return large data structures that cannot be returned by value. However, it must be done carefully to avoid memory leaks, dangling pointers, and other issues related to dynamic memory management. In this article, we will 2 min read How to Implement a Copy Constructor for Deep Copying? In C++, the copy constructor enables us to initialize an object using another object of the same class. In this article, we will learn, how to implement a copy constructor for deep copying. Implementing a Copy Constructor for Deep CopyingIn C++, a copy constructor is used to create a copy of an obje 3 min read Deletion of array of objects in C++ Need for deletion of the object: To avoid memory leak as when an object is created dynamically using new, it occupies memory in the Heap Section.If objects are not deleted explicitly then the program will crash during runtime. Program 1: Create an object of the class which is created dynamically usi 3 min read How to Remove an Element from a List in C++? In C++, the STL provides a std::list container that represents a doubly linked list to store the sequential data in non-contiguous memory locations. In this article, we will learn how to remove an element from a list in C++. Example: Input: myList = {1, 2, 3, 4, 5, 6, 7, 8} Target = 5 Output: // rem 2 min read How to Delete a Pointer in C++? In C++, memory management is essential to avoid memory leaks, crashes, and undefinable behavior. In this article, we will learn how to properly delete a pointer in C++. Deleting a Pointer in C++By deleting a pointer, we refer to deleting the memory allocated to the pointer. To delete a pointer in C+ 2 min read How to Create Deque of User-Defined Data Type in C++? In C++, deque is a container in which we can insert and delete at both its beginning and its end. It supports almost all kinds of inbuilt data types. In this article, we will learn how to create a deque of user-defined data types in C++. Example Input://user defined data typestruct usd { int a; char 2 min read How to Copy a One Deque to Another in C++? In C++, STL provides a container called deque (short for double-ended queue) that allows fast insertion and deletion at both its beginning and its end. In some scenarios, we may need to copy the contents of one deque to another. In this article, we will learn how to copy one deque to another in C++. 2 min read How to Define a Move Constructor in C++? In C++, we have a move constructor which is a part of C++11 move semantics and is used to handle resources for temporary and rvalue objects. In this article, we will learn how to write a move constructor in C++. How to Write Move Constructor in C++?The move constructor is defined similarly to a copy 2 min read How to Delete a File in C++? C++ file handling allows us to manipulate external files from our C++ program. We can create, remove, and update files using file handling. In this article, we will learn how to remove a file in C++. Delete a File in C++ To remove a file in C++, we can use the remove() function defined inside the 2 min read Like