Meaning of = delete after function declaration
class my_class
{
...
my_class(my_class const &) = delete;
...
};
What does = delete
mean in that context?
Are there any other "modifiers" (other than = 0
and = delete
)?
Solution 1:
Deleting a function is a C++11 feature:
The common idiom of "prohibiting copying" can now be expressed directly:
class X { // ... X& operator=(const X&) = delete; // Disallow copying X(const X&) = delete; };
[...]
The "delete" mechanism can be used for any function. For example, we can eliminate an undesired conversion like this:
struct Z { // ... Z(long long); // can initialize with an long long Z(long) = delete; // but not anything less };
Solution 2:
-
= 0
means that a function is pure virtual and you cannot instantiate an object from this class. You need to derive from it and implement this method -
= delete
means that the compiler will not generate those constructors for you. AFAIK this is only allowed on copy constructor and assignment operator. But I am not too good at the upcoming standard.