boost::pointer_traits使用详解
boost::pointer_traits是一个实用的模板类,它提供了一种可以在不知道指针类型的情况下访问该指针所指向对象的工具。boost::pointer_traits 能够在不同的平台和编译器上使用,并且它可以与 C++11 的原生指针类型兼容。
boost::pointer_traits 为我们提供了以下类型:
(1)typedef Pointer element_type;
(2)typedef std::ptrdiff_t difference_type;
(3)typedef Pointer rebind::other pointer_to_other; // 其中 U 为新的指向类型
(4)static Pointer pointer_to(T& r);
(5)static Pointer pointer_to(const T& r);
(6)static void pointer_assign(Pointer& p, T* r); // 类似于 p = r
(7)static void pointer_deleter(Pointer& p); // 释放指向的内存
下面是一个示例程序:
#include
#include