当前位置: 动力学知识库 > 问答 > 编程问答 >

Is there any use in assigning NULL to a pointer after deleting it in destructor in C++?

问题描述:

Possible Duplicate:

Is it worth setting pointers to NULL in a destructor?

I see some code like this,

void ClassA::~ClassA()

{

delete member;

member = NULL;

}

as the particular instance doesn't exist anymore after this destructor (or the instance is destructed and its members can't be used or dereferenced anymore), what is the use of assigning NULL to the pointer of member variable?

Is it just a practice taken from deleting a pointer elsewhere and assigning NULL to it?

网友答案:

I see no reason for updating data which is already out of scope, and it is NOT good practice to do things "just in case". At any rate do not follow "good practices" blindly, it is better to understand why you do things than being the most fervours practitioner of the "good practices" religion.

网友答案:

It's pretty pointless - the space isn't going to be used again until it is reallocated.

网友答案:

It's pointless for release code, but potentially helpful for debugging.

网友答案:

yes, this is just good practice. helps one not to forget to set pointers to null elsewhere ;)

分享给朋友:
您可能感兴趣的文章:
随机阅读: