简体   繁体   中英

Why must a pure virtual destructor's implementation be empty? And should it be inline?

I read in other threads that when you implement a pure virtual destructor (yes it can have an implementation) it must be empty, and should (?) be inline. Should it be empty? If so, why? Should it be inline? If so, why?

Edit: This is how a pure virtual descructor can have an implementation:

class A{
    virtual ~A() = 0;
}

inline A::~A(){
    //implementation
}

A pure virtual destructor must have an implementation (assuming you have at least one concrete derived class).

There is no rule that a pure virtual destructor must have empty body. Nor do I know of any reason that it should, except the same reasons most destructors should have an empty body.

A pure virtual destructor can be inline or non-inline. I would expect the benefits of each to depend on the number of base classes and non-static members with non-trivial destructors.

One other catch, though: on certain popular compilers, if the destructor is the only virtual method defined for the class, it is a good idea to make it non-inline, to help the implementation deal with its polymorphism magic.

Sounds strange.

Firstly, "yes it can have an implementation" is a rather strange remark. It is actually required to have an implementation (at least in C++98). There's no way around it. Anyone who ever used a pure virtual destructor knows it.

Secondly, it is not supposed to always be empty. It is simply supposed to do what it needs to do. If you have nothing to do there explicitly, leave it empty. Otherwise, it won't be empty.

Finally, it can be inline or non-inline - it makes no difference. What is true is that it cannot be defined in class definition. The definition must be out-of-class one (inline or not - does not matter).

You need implementation of pure virtual destructor because, the way virtual destructor works is that the most derived class's destructor is called first, then the destructor of each base class is called. It means compilers will generate a call to base class pure virtual destructor even though the class is abstract, so you must provide a body for the function. If you don't provide body, the linker will complain about a missing symbol.

There might be a case, when you want your base class to be abstract even if your class does not contain any pure virtual function. Here declaring pure virtual destructor will make your class abstract. In this case, your destructor can have empty body. To avoid paying overhead cost of a call to an empty body destructor,declare it as inline.

Keeping in mind that 'must' and 'should' mean different things...

A pure virtual destructor can be non-empty. Don't know who might have said otherwise.

Should it be? Yes, since an abstract base should not have anything to delete. You will find that you'll violate this latter 'should' from time to time and thus, of course, may the former as well.

Should it be inline? It neither should nor should not. I can't think of anything you clearly gain by it being "inline" since implementations are both free to ignore inlines and to inline non-inlines. Generally though there's no reason to create an implementation file for nothing more than an empty destructor in an abstract class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM