OpenCV中的Ptr解析
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模板类就是采用分离引用计数型的句柄类实现技术。
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇:没有了
- 下一篇:没有了