c++ - NULL 在类析构函数中

标签 c++ class null destructor explicit

<分区>

Possible Duplicate:
Is it worth setting pointers to NULL in a destructor?

在析构函数中将指针(分配堆内存)设置为 NULL 是否毫无意义?

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

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

        int *Value;
};

关于类,什么时候应该使用explicit关键字?

最佳答案

是的。这是没有意义的,因为对象正在被销毁。 一旦销毁,指针将无法到达。

关于c++ - NULL 在类析构函数中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4984916/

相关文章:

C++如何制作这种构造函数的原型(prototype)?

javascript - Angular:仅在当前单击的按钮上切换事件类(不使用 *ngFor)

c++ - 使用 `dynamic_cast` 来推断在基类上定义并在派生类上实现的成员函数的参数类型是否正确?

Python 和类属性声明

java - 声明类型不能为 null

c++ - 在 C++ 中使用单例

c++ - Rapidjson中Move的使用

c++ - 内部类和外部成员的访问

带有传递的python函数

c - C中与NULL的两次相等比较