Is it good practice to NULL a pointer after deleting it?

后端 未结 18 881
猫巷女王i
猫巷女王i 2020-11-22 11:28

I\'ll start out by saying, use smart pointers and you\'ll never have to worry about this.

What are the problems with the following code?

<         


        
18条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 11:36

    If there is more code after the delete, Yes. When the pointer is deleted in a constructor or at the end of method or function, No.

    The point of this parable is to remind the programmer, during run-time, that the object has already been deleted.

    An even better practice is to use Smart Pointers (shared or scoped) which automagically delete their target objects.

提交回复
热议问题