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

要弄懂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


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


  \note{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<>就是一个模板类。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值