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

前端 未结 4 852
春和景丽
春和景丽 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 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.

提交回复
热议问题