Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ function = delete

In C++ (since C++ 11 I believe), it is possible to "delete" constructors, or assignment operators, whenever the programmer does not want the compiler to automatically implement a default constructor, or assignment operator.

For example, one may,

class a
{
    const a& operator=(const a& copy_me) = delete;
}

which will remove the default assignment operator. The compiler will not implement a default operator= for us. We simply will not be able to us it in our code, it will be as if it was never there.

Consider an abstract base class (or just a base class) with functions, virtual functions and pure virtuals.

class example_base
{
    void function()
    {
        return;
    }

    virtual
    void virtual_function() // Do we have to provide an implementation?
    {
        return;
    }

    virtual
    void pure() = 0; // Are there other ways to declare a pure virtual?
                     // Can pure() have an implementation in this class?
}

If another class inherits from the base class, are we allowed to "delete" some of the functions provided in the base? (Q1)

If so, what are the rules and effects of this? (Q2)

What will happen if a subsequent class inherits from example_inherits and we have deleted some of the member functions? Are we allowed to use them again? (Q3)

class example_inherits : example_base
{
    void function() = delete; // Allowed?

    virtual
    void virtual_function() = delete; // If so why ...

    virtual
    void pure() = delete; // ... or why not?
}

Example of Q3:

class another : example_inherits
{
    // Allowed to use deleted function?
    void function()
    {
        std::cout << "blaa" << std::endl;
    }
}

You can probably think of other permutations of what I described, such as implementing a deleted method 2 orders of inheritance lower. (ie, in a class which inherits from a class which inherits from a class...)

Or different choices of virtual / pure virtual orderings... eg: one class has a virtual pure, something inherits from this class and inherits the same function as a virtual, then another class which inherits from this either deletes it or implements it as a regular function... etc...

Some insight into this would be appreciated - I wasn't able to find information this detailed online.

like image 232
FreelanceConsultant Avatar asked Jul 26 '15 10:07

FreelanceConsultant


2 Answers

Well, you have quite a lot of questions here. And they all could have been answered by trying it :)

We simply will not be able to us it in our code, it will be as if it was never there

Not quite, because the deleted version participates in overload resolution. For example:

void f(long);
void f(int) = delete;

f(5);

This will fail to compile, however if you comment out the =delete version then it compiles.


Moving onto your questions.

Can pure() have an implementation in this class?

Yes, = 0 only means that derived classes must override it. But you can still provide an out-of-line definition for that function in the same class.

void function() = delete; // Allowed?

Yes, this is allowed. The derived function() hides the base function(), since it is not virtual. You could of course still call the base function through a pointer or reference to the base class.

void virtual_function() = delete;

This is not allowed; virtual functions may not be deleted.

like image 96
M.M Avatar answered Sep 23 '22 12:09

M.M


Matt's answer covers almost everything, but I can answer the question on why you cannot delete virtual functions (Q3):

The entire point of virtual functions is to enable subtype polymorphism. A sane implementation of this implies that you can use any legally implemented subtype in any legally implemented function that is written using the base type. This is a rephrasing of the Liskov Substitution Principle.

This means if you were able to delete a virtual function in a subclass, this subclass would create undefined behavior when used in a legally implemented function. Not exactly desirable, and I can't imagine a situation where it would give any benefit either.

like image 24
audun Avatar answered Sep 21 '22 12:09

audun