#include <copy-traits.h>
Inheritance diagram for copy_traits_base:
Static Public Methods | |
T * | clone (T const &t) |
void | destroy (T *t) |
If we have a pointer to an abstract base of a polymorphic type, we cannot copy the content by simply calling operator new
. Instead, we must use some virtual member function like clone()
. This is a problem when using polymorphic types together with smart pointers (how do they copy the pointer?). Also, if we want to use the 'pimpl' idiom together with a smart pointer, we cannot call delete on the opaque pointer-to-impl inside the smart pointer.
These differences are hidden by T* copy_traits<T>::clone(const T&)
and copy_traits<T>::destroy(T*)
Default implementation in copy_traits_base for non-polymorphic types is simply operator new / delete.
For T being an abstract class, this implmentation has to be overridden with a method like T::clone()
by specializing copy_traits<>
for T.
For an opaque type, we can declare a specialization along with the forward declaration of the type:
class impl; template<> struct copy_traits<impl> { static impl* clone (impl const&); static void destroy(impl*); };
and define the specialization with the definition of the type:
class impl { ... }; impl* copy_traits<impl>::clone (impl const& i) { return new impl(i);} void copy_traits<impl>::destroy(impl * i) { delete i;}
Definition at line 56 of file copy-traits.h.
|
Reimplemented in copy_traits< testA >. Definition at line 57 of file copy-traits.h. |
|
Definition at line 58 of file copy-traits.h. |