假如 SmartPtr 跟随 auto_ptr 和 tr1::shared_ptr 的脚步,提供一个返回被这个 smart pointer(智能指针)持有的 built-in pointer(内建指针)的拷贝的 get member function(get 成员函数)(参见《 C++箴言:在资源管理类中准备访问裸资源 》),我们可以用 constructor template(构造函数模板)的实现将转换限定在我们想要的范围:
template
class SmartPtr {
public:
template
SmartPtr(const SmartPtr
& other) // initialize this held ptr
: heldPtr(other.get()) { ... } // with others held ptr
T* get() const { return heldPtr; }
...
private: // built-in pointer held
T *heldPtr; // by the SmartPtr
};
我们通过 member initialization list(成员初始化列表),用 SmartPtr 持有的类型为 U* 的指针初始化 SmartPtr
template
class shared_ptr {
public:
template
// construct from
explicit shared_ptr(Y * p); // any compatible
template
// built-in pointer,
shared_ptr(shared_ptr
const& r); // shared_ptr,
template
// weak_ptr, or
explicit shared_ptr(weak_ptr
const& r); // auto_ptr
template
explicit shared_ptr(auto_ptr
& r);
template
// assign from
shared_ptr& operator=(shared_ptr
const& r); // any compatible
template
// shared_ptr or
shared_ptr& operator=(auto_ptr
& r); // auto_ptr
...
};
除了 generalized copy constructor(泛型化拷贝构造函数),所有这些 constructors(构造函数)都是 explicit(显式)的。这就意味着从 shared_ptr 的一种类型到另一种的 implicit conversion(隐式转换)是被允许的,但是从一个 built-in pointer(内建指针)或其 smart pointer type(智能指针类型)的 implicit conversion(隐式转换)是不被许可的。(explicit conversion(显式转换)――例如,经由一个 cast(强制转型)――还是可以的。)同样引起注意的是 auto_ptrs 被传送给 tr1::shared_ptr 的 constructors(构造函数)和 assignment operators(