入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

OpenCV中Ptr<>的应用的几点问题

创建时间:2017-04-27 投稿人: 浏览次数:1830

要弄懂Ptr<>代表什么意思,首先要弄懂两个概念,一个是智能指针,一个是模板类。

智能指针

智能指针的概念可以搜索别的文章,这里Ptr<>模板类就是一种智能指针,也可以理解成一个指针,或者是一个指针类,可以防止内存泄漏等问题,比普通指针好用。如果要查看该类的源码,可以f12查看。在该类Ptr的上面,有一段这样的说明: Smart pointer to dynamically allocated objects.


  This is template pointer-wrapping class that stores the associated reference counter along with the
  object pointer. The class is similar to std::smart_ptr<> from the recent addons to the C++ standard,
  but is shorter to write :) and self-contained (i.e. does add any dependency on the compiler or an external library).


  Basically, you can use "Ptr<MyObjectType> ptr" (or faster "const Ptr<MyObjectType>& ptr" for read-only access)
  everywhere instead of "MyObjectType* ptr", where MyObjectType is some C structure or a C++ class.
  To make it all work, you need to specialize Ptr<>::delete_obj(), like:


  code
  template<> void Ptr<MyObjectType>::delete_obj() { call_destructor_func(obj); }
  endcode


  ote{if MyObjectType is a C++ class with a destructor, you do not need to specialize delete_obj(),
  since the default implementation calls "delete obj;"}


  ote{Another good property of the class is that the operations on the reference counter are atomic,
  i.e. it is safe to use the class in multi-threaded applications}
大概就是说他的用法等。

模板类

就是一个类,他的参数可以是任何类型的,要用的时候临时指定一下就能用了。比如这一句Ptr<BaseColumnFilter> _columnFilter;,Ptr<BaseColumnFilter>就是一个模板类,尖括号里面的就是临时制定的参数,表示定义的变量_columnFilter是一个指向BaseColumnFilter对象的一个指针,Ptr<>就是一个模板类。
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
  • 上一篇:没有了
  • 下一篇:没有了
未上传头像