Do I need to nullify a member variable in the destructor?

后端 未结 3 1709
我在风中等你
我在风中等你 2021-01-18 11:19

Why one would want to explicitly clear the a vector member variable (of on in a dtor (please see the code below). what are the benefits of clearing the vector, even though i

3条回答
  •  醉梦人生
    2021-01-18 11:35

    In your second example there's no reason to set p_ to null whatsoever, specifically because it is done in the destructor, meaning that the lifetime of p_ will end immediately after that.

    Moreover, there's no point in comparing p_ to null before calling delete, since delete expression performs this check internally. In your specific artificial example, the destructor should simply contain delete p_ and noting else. No if, no setting p_ to null.

提交回复
热议问题