How to safely delete multiple pointers

前端 未结 8 1907
無奈伤痛
無奈伤痛 2021-01-03 02:06

I have got some code which uses a lot of pointers pointing to the same address. Given a equivalent simple example:

int *p =  new int(1);
int *q = p;
int *r =         


        
8条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-03 02:25

    The "modern" answer is to use a smart pointer and don't do any manual deletes.

    boost::shared_ptr p(new int(1));
    boost::shared_ptr q = p;
    boost::shared_ptr r = q;
    

    End of story!

提交回复
热议问题