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

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

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

    I don't have a good answer, but I have simplified the issue slightly. The following code removes the operator new and exception handling:

    #include 
    using namespace std;
    
    class Widget {
    
      public:
        Widget() {
            cout<<"Widget()"<

    This still exhibits the same behaviour and des so on both VC++ and g++.

    Of course, deleting a NULL pointer is a no-op, so the compiler does not have to call operator delete. If one actually allocates an object:

        Widget* w = new Widget;
    

    then things work as expected.

提交回复
热议问题