NULL In the destructor class

Possible duplicate:
Is it worth pointing to pointers to NULL in the destructor?

Does it make sense to point the pointer (which allocates heap memory) to NULLin the destructor?

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

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

        int *Value;
};

Whereas regarding classes, when should I use the keyword explicit?

+3
source share
4 answers

Yes. This is pointless because the object is in the process of destruction. Once it is destroyed, there will be no path to the pointer.

+5
source

Yes, it makes no sense to point to the pointer at NULLthe very end of the destructor, since it will no longer exist as soon as you leave the destructor.

, .: -)

, MyClass int f(const MyClass&), f

f(42)

f(MyClass(42))

, . .

+4

NULL.

- SampleClass, ( ), , - , NULL ( , ).

Otherwise, you can continue to use the remote object and notice an error much later (when the memory is overwritten by another object). Such problems are often very difficult to debug.

+1
source

This is not meaningless. Although it is true that, theoretically, the value will never be available again, setting it to NULL may help you in the future if any other fraud begins.

+1
source

Source: https://habr.com/ru/post/1791841/


All Articles