Virtual destructor and undefined behavior

前端 未结 4 1399
清歌不尽
清歌不尽 2020-11-28 13:27

This question is different than \'When/why should I use a virtual destructor?\'.

struct B {
  virtual void foo ();
  ~B() {}  // <--         


        
4条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-28 14:16

    1. Undefined Behavior
    2. (One first note, these deconstructors are generally not as empty as you would think. You still have to deconstruct all of your members) Even if the deconstructor is truly empty(POD?), then it still depends on your compiler. It is undefined by the standard. For all the standard cares your computer could blow up on the delete.
    3. Undefined Behavior

    There really is no reason for a non-virtual public destructor in a class that is meant to be inherited from. Look at this article, Guideline #4.

    Use either a protected non-virtual destructor and shared_ptrs(they have static linking), or a public virtual destructor.

提交回复
热议问题