NULL在类析构函数中

Dea*_*tor 3 c++ null destructor explicit class

可能重复:
是否值得在析构函数中设置指向NULL的指针?

NULL在析构函数中设置指针(分配堆内存)是没有意义的吗?

class SampleClass
{
    public:
        SampleClass( int Init = 0 )
        {
            Value = new int( Init );
        }

        ~SampleClass( void )
        {
            delete Value;
            Value = NULL; // Is this pointless?
        }

        int *Value;
};
Run Code Online (Sandbox Code Playgroud)

关于课程的主题,我explicit什么时候应该使用关键字?

Evi*_*ach 5

是.这是毫无意义的,因为对象是在被破坏的过程中.一旦它被销毁,就无法到达指针.

  • @mzabsky如果遇到这个问题,最好使用更智能的指针,这样就不会过早删除对象.试图在程序的其余部分防范错误是非常没有意义的. (3认同)