``makePtr<T>(...)`` is equivalent to ``Ptr<T>(new T(...))``. It is shorter than the latter, and
it's marginally safer than using a constructor or :ocv:func:`Ptr::reset`, since it ensures that
the owned pointer is new and thus not owned by any other ``Ptr`` instance.
Unfortunately, perfect forwarding is impossible to implement in C++03, and so ``makePtr`` is limited
to constructors of ``T`` that have up to 10 arguments, none of which are non-const references.
Mat
---
...
...
@@ -2967,7 +3108,7 @@ Creates algorithm instance by name
:param name: The algorithm name, one of the names returned by ``Algorithm::getList()``.
This static method creates a new instance of the specified algorithm. If there is no such algorithm, the method will silently return null pointer (that can be checked by ``Ptr::empty()`` method). Also, you should specify the particular ``Algorithm`` subclass as ``_Tp`` (or simply ``Algorithm`` if you do not know it at that point). ::
This static method creates a new instance of the specified algorithm. If there is no such algorithm, the method will silently return a null pointer. Also, you should specify the particular ``Algorithm`` subclass as ``_Tp`` (or simply ``Algorithm`` if you do not know it at that point). ::
@@ -83,17 +83,22 @@ First of all, ``std::vector``, ``Mat``, and other data structures used by the fu
// matrix will be deallocated, since it is not referenced by anyone
C = C.clone();
You see that the use of ``Mat`` and other basic structures is simple. But what about high-level classes or even user data types created without taking automatic memory management into account? For them, OpenCV offers the ``Ptr<>`` template class that is similar to ``std::shared_ptr`` from C++ TR1. So, instead of using plain pointers::
You see that the use of ``Mat`` and other basic structures is simple. But what about high-level classes or even user
data types created without taking automatic memory management into account? For them, OpenCV offers the :ocv:class:`Ptr`
template class that is similar to ``std::shared_ptr`` from C++11. So, instead of using plain pointers::
T* ptr = new T(...);
you can use::
Ptr<T> ptr = new T(...);
Ptr<T> ptr(new T(...));
That is, ``Ptr<T> ptr`` encapsulates a pointer to a ``T`` instance and a reference counter associated with the pointer. See the
:ocv:class:`Ptr`
description for details.
or::
Ptr<T> ptr = makePtr<T>(...);
``Ptr<T>`` encapsulates a pointer to a ``T`` instance and a reference counter associated with the pointer. See the