Deleted functions

In C++11 is it possible to remove a function from a class by using the delete specifier. If you try to use a deleted function will give a compiler error.

As an example, the following class is made non-movable by making it impossible to call the move operator and move constructor:

We can also stop anyone from creating instances of a class on the free store (heap)  by deleting the new operator:

Or more compact by using a template

To stop users from calling a method with specific parameters, delete the functions with the not allowed parameters:

 

Facebookgoogle_pluslinkedinmail

Override

The override specifier was added in C++11 and is used to show that a method is overriding another method in a base class. When you use this specifier the compiler will help you to make sure that you are using the same signature in the new method as in the base class method.

Examples:

 

Why should we use this?

 

  • Because this makes your code more clear, adding override to your function makes it clear to anyone reading the code later that this is an override of a base class function.
  • It is very easy to accidentally create a new virtual function in a derived class when you intended to override a function in a base class by making an error in the signature of the function (like forgetting a const).
Facebookgoogle_pluslinkedinmail