Delete keyword on functions in C++ 11

C-Plus-Plus

In this article, we are going to learn about another new feature Delete keyword on functions in C++ 11. It is a language standard to disable certain functions by using the delete keyword. This is a new introduction in C++ 11 where we can delete a class member function.

1. What is Delete keyword in C++


Here delete means we are asking the compiler to eliminate that particular function from the class and no one will be able to call the deleted function. If we try to call a deleted function then this will result in a compilation error. This is a very useful and powerful feature that helps in application designs and helps in many ways. Some of the most helpful use of this feature is: This feature is useful for preventing copies of objects and more. so mainly we can make use of the delete keyword for:

2. Features of Delete keyword on functions in C++


  • To Delete compiler-generated functions default constructor, copy constructor, move constructor, assignment operators & move assignment operator.
  • To Disabling undesirable argument conversion.

As some of you might be aware that when we create a class, the compiler helps us by providing some of the default functions for that class. These default functions are like default constructor, default destructor, copy constructor, move constructor, etc.

If we do not want any of these default functions due to any reason like a design demand or something like that, then we can make use of the ‘delete’ keyword. Deleting special member functions is a way to prevent the compiler from generating special member functions. So the functions don’t even get generated. Let us understand this with an example, we defined the class as below :

C++ Program to use delete keyword

In our example class, as you can see we have some functions, one is a constructor and another one is = assignment operator. Also, we have added the “= delete;” keyword at the end of the assignment operator function declaration.

class sampleclass
{
  public:
      sampleclass(int var){};
      sampleclass(){} = default;
       sampleclass& operator=(const sampleclass&) = delete;  // assignment operator deleted
};

3. How it works?


This delete keyword has deleted the default assignment operator function from this class and now this class does not have an assignment operator. To confirm that if we try to execute the below lines of code we will get an error that no assignment operator is available.

Program Example

sampleclass obj(5);     // calling the constructor with int.
sampleclass obj1;       // calling default constructor.
obj1 = obj;             // Compilation Error

As you can see in the third line we intended to call the assignment operator but the compiler is complaining that no assignment operator is available because we have deleted that by using the delete keyword. Similarly, we can delete the copy constructor also by adding the below line to our class.

sampleclass(const sampleclass&) = delete;

Now if we try the below code then the compiler will give us an error that no copy constructor is available and we are trying to call the deleted copy constructor.

sampleclass obj1 = obj; // Compilation Error 

4. Disabling undesirable argument conversion


There are other uses also of ‘delete’ keyword , which are indirect uses and helps us get some desired results when we want to put some restrictions on our code.One of the examples is, to restrict the typecasting on values. Let us understand this with the example below:

Progarm Example

In this example, we are define a class, which has a constructor has an int argument. But as per our need, we do not want anyone to pass a double argument to this constructor. So we will need to do some magic to restrict this because, at run time, any double value passed may be accepted by run-time value conversion.

Our magic is to use the ‘ delete’ keyword on the double version of this constructor which will ensure no one will be able to pass double values to this constructor and get the object created.

class sampleclass
{
public:
    sampleclass(int var){};
    sampleclass(double var) = delete; 
};

sampleclass obj(101.25);   // Error: conversion from double to int disabled

Summary


To summarize what we learned in this article, we can make use of the delete keyword to help us in making some great design patterns or some restrictions on our code.