STL class for reference-counted pointers?

我们两清 提交于 2019-12-03 11:06:46

With the exception of the already mentionned TR1 shared_ptr, there is no reference-counted pointer in STL.

I suggest you use boost::shared_ptr (downloading boost will be enough, there is nothing to compile, its implementation is header-only).

You may also want to have a look at smart pointers from Loki libraries (again, header-only implementation).

For an overview of available smart pointers, see there.

If you don't want/can't use Boost and your compiler implements TR1, you can use shared_ptr (borrowed from Boost):

#include <tr1/memory>

...

std::tr1::shared_ptr<Foo> ptr(new Foo);

Otherwise, no, there are no smart pointers except std::auto_ptr in vanilla STL.

Time marches on, in C++11:

std::shared_ptr
std::weak_ptr

etc.

For COM objects, use CComPtr<>.

There is an alternative implemented for some time in STL PLUS, see at source forge

"STLplus was originally intended as a library to extend the STL by providing missing container data structures such as smart-pointers, matrices, trees and graphs."

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!