delete a NULL pointer does not call overloaded delete when destructor is written

前端 未结 7 2150
慢半拍i
慢半拍i 2021-02-02 15:05
class Widget
{
    public:
        Widget() {
            cout<<\"~Widget()\"<

        
7条回答
  •  甜味超标
    2021-02-02 15:31

    The object destructor is called before the delete operator. So my guess would be that it tries to call the destructor, realizes that the pointer is NULL therefore

    1. doesn't call destructor which needs an instance
    2. stops the deleteing operation there (kind of speed optimization IMHO).

    As Neil said, if w contains a Widget, it should work.

提交回复
热议问题