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

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

提交回复
热议问题