In programming, some of the most important parts are the functions that we create. They are like Lego parts that we can use in all places of the application. In modern C++ development, a function declaration is really important, and these functions gained some additional abilities after the C++11 standards. C++11 introduced two new features: defaulted and deleted functions. These are very useful in the function members of classes that we call methods. In this post, we explain what is deleted function is in modern C++.
Table of Contents
What is a function in C++?
In modern C++ development, a function declaration is really important. In C++, a function declaration is made by using the function name and its return type A function definition associates the function name/type with the function body.
Since C++11, a function declaration syntax with the full options is as shown below.,
Here is a simple function that return sum of a and b variables.
What is a deleted function in modern C++?
C++11 introduced 2 new features for functions – defaulted and deleted functions. These are very useful in the function members of classes that we call methods. Here is the syntax how to delete a function in a class or struct.
A deleted function is a function that contains =delete;
in its prototype. This construction, introduced with C++11, indicates that the function may not be used. This construction can be used to forbid the usage of default language facilities (like default constructors or default operators) or problematic conversions.
and here is an example,
this function is deleted and cannot be used in any future usage after this declaration.
Is there a simple deleted function example in C++?
A simple empty C++ class is perfectly equivalent to default implementations (Rule of Six) in a class, it has constructor and other 5 default members. For example, we can set delete
for the default constructor of a class, in compilation this class will not have that default constructor. Here is how to do it:
If you don’t want your class copy:
Is there a full deleted function example in C++?
Now, let’s use delete
keyword in all 6 rules of C++ classes,
While the delete keyword is easy to use, there are many different usage options. Please search deleted in LearnCPlusPlus.org for more examples about deleted methods.
For more information on this feature, see Deleted functions Proposal document.
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise version.