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

后端 未结 18 1080
猫巷女王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:46

    If the code does not belong to the most performance-critical part of your application, keep it simple and use a shared_ptr:

    shared_ptr p(new Foo);
    //No more need to call delete
    

    It performs reference counting and is thread-safe. You can find it in the tr1 (std::tr1 namespace, #include < memory >) or if your compiler does not provide it, get it from boost.

提交回复
热议问题