Disadvantages of shared_ptr

前端 未结 3 921
粉色の甜心
粉色の甜心 2021-02-14 08:15

With shared_ptr included in c++11, one could achieve a semi garbage-collected enviroment. Does the (inflationary?) usage come along with some disadvantages?

I could imag

3条回答
  •  再見小時候
    2021-02-14 08:47

    I performance is important I wold not use std::sahred_ptr. For example, to sort an array may with real pointers is 5 times faster than to sort with shared pointers.

    On the other hand shared pointer do not avoid all troubles with garbage leaks in case of circular references. Of course, this can be solved with weak_ptr, but what I mean is, that shared pointer need also be handled carfully.

    I prefer to use shared_ptr for static elements, because static elements are not deleted by a destructor of a class.

提交回复
热议问题