您的位置:首页 > 运维架构

OpenCV中的Ptr解析

2013-11-15 16:36 288 查看
OpenCV里面的Ptr的定义如下:Template class for smart reference-counting pointers,即智能指针模板类。The Ptr<_Tp> class is a template class that wraps
pointers of the corresponding type. It is similar to shared_ptr that is part of the Boost library (http://www.boost.org/doc/libs/1_40_0/libs/smart_ptr/shared_ptr.htm) and also part of the C++0x standard.

This class provides the following options:
Default constructor, copy constructor, and assignment operator for an arbitrary C++ class or a C structure. For some objects, like files, windows, mutexes, sockets, and others, a copy
constructor or an assignment operator are difficult to define. For some other objects, like complex classifiers in OpenCV, copy constructors are absent and not easy to implement. Finally, some of complex OpenCV and your own data structures may be written in
C. However, copy constructors and default constructors can simplify programming a lot.Besides, they are often required (for example, by STL containers). By wrapping a pointer to such a complex object TObj to Ptr<TObj>,
you automatically get all of the necessary constructors and the assignment operator.
O(1) complexity of the above-mentioned operations. While some structures, likestd::vector,
provide a copy constructor and an assignment operator, the operations may take a considerable amount of time if the data structures are large. But if the structures are put into Ptr<>,
the overhead is small and independent of the data size.
Automatic destruction, even for C structures. See the example below with FILE*.
Heterogeneous collections of objects. The standard STL and most other C++ and OpenCV containers can store only objects of the same type and the same size. The classical solution to
store objects of different types in the same container is to store pointers to the base class base_class_t* instead but then you loose the
automatic memory management. Again, by using Ptr<base_class_t>() instead
of the raw pointers, you can solve the problem.
cast pointer to another type

template<typename _Tp2> Ptr<_Tp2> ptr();
template<typename _Tp2> const Ptr<_Tp2> ptr() const;

OpenCV中的智能指针Ptr模板类就是采用分离引用计数型的句柄类实现技术。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: