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

前端 未结 4 865
春和景丽
春和景丽 2021-02-19 10:30

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

相关标签:
4条回答
  • 2021-02-19 10:48

    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.

    0 讨论(0)
  • 2021-02-19 11:06

    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).

    0 讨论(0)
  • 2021-02-19 11:08

    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.

    0 讨论(0)
  • 2021-02-19 11:12

    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.

    0 讨论(0)
提交回复
热议问题