More Effective C++的前言、导读和附1(侯捷译),以及在 “C++ 中计算物件个数”和“为智能指标实作 operator->*”(陈崴译,原发表于程序员杂志),可在侯捷的站点下载到。
一个auto_ptr的实现实例 Items M9、M10、E26、E31和E32证明了auto_ptr模板类的非同寻常的作用。不幸的是,目前很少有编译器地提供了一个“正确”的实现(注1)。Items M9和M28大致描述了你怎么自己实现一个,但从事实际项目时有一个更详尽的版本就太好了。 下面是两个auot_ptr的实现。第一个版本文档化了类的接口并在类的定义体外面实现了所有的成员函数。第二个版本将所有的成员函数都实现在定义体内了。在文体上,第二个实现不如第一个,因为它没有将类的接口从实现中分离出来。但,auto_ptr只是一个简单的类,所以第二个实现比第一个清晰得多。 这是有专门接口申明的auto_ptr模板: template<class T> class auto_ptr { public: explicit auto_ptr(T *p = 0); // Item M5 有“explicitfor” // 的描述 template<class U> // 拷贝构造函数成员模板 auto_ptr(auto_ptr<U>& rhs); // (见Item M28): // 用另一个类型兼容的 // auto_ptr对象 // 初始化一个新的auto_ptr对象 ~auto_ptr(); template<class U> // 赋值操作成员模板 auto_ptr<T>& // (见Item M28): operator=(auto_ptr<U>& rhs); // 用另一个类型兼容的 // auto_ptr对象给它赋值 T& operator*() const; // 见Item M28 T* operator->() const; // 见Item M28 T* get() const; // 返回包容指针的 // 当前值 T* release(); // 放弃包容指针的 // 所有权, // 并返回其当前值 void reset(T *p = 0); // 删除包容指针, // 获得指针p的所有权 private: T *pointee; template<class U> // 让所有的auto_ptr类 friend class auto_ptr<U>; // 成为友元 }; template<class T> inline auto_ptr<T>::auto_ptr(T *p) : pointee(p) {} template<class T> inline auto_ptr<T>::auto_ptr(auto_ptr<U>& rhs) : pointee(rhs.release()) {} template<class T> inline auto_ptr<T>::~auto_ptr() { delete pointee; } template<class T> template<class U> inline auto_ptr<T>& auto_ptr<T>::operator=(auto_ptr<U>& rhs) { if (this != &rhs) reset(rhs.release()); return *this; } template<class T> inline T& auto_ptr<T>::operator*() const { return *pointee; } template<class T> inline T* auto_ptr<T>::operator->() const { return pointee; } template<class T> inline T* auto_ptr<T>::get() const { return pointee; } template<class T> inline T* auto_ptr<T>::release() { T *oldPointee = pointee; pointee = 0; return oldPointee; } template<class T> inline void auto_ptr<T>::reset(T *p) { if (pointee != p) { delete pointee; pointee = p; } } 这是所有函数定义在类定义体内的auto_ptr模板。如你所见,它不会搞乱大脑: template<class T> class auto_ptr { public: explicit auto_ptr(T *p = 0): pointee(p) {} template<class U> auto_ptr(auto_ptr<U>& rhs): pointee(rhs.release()) {} ~auto_ptr() { delete pointee; } template<class U> auto_ptr<T>& operator=(auto_ptr<U>& rhs) { if (this != &rhs) reset(rhs.release()); return *this; } T& operator*() const { return *pointee; } T* operator->() const { return pointee; } T* get() const { return pointee; } T* release() { T *oldPointee = pointee; pointee = 0; return oldPointee; } void reset(T *p = 0) { if (pointee != p) { delete pointee; pointee = p; } } private: T *pointee; template<class U> friend class auto_ptr<U>; }; 如果你所用的编译器还不支持“explicit”,可以安全地用#define取消它的存在: #define explicit 这不会造成auto_ptr的任何功能减弱,但导致轻微的安全性减弱。详见Item M5。 如果你的编译器不支持成员模板,你可以使用非模板的auto_ptr拷贝构造函数和赋值操作(描述在Item M28)。这将造成你的auto_ptr在使用上有些小小的不方便,但没有其它方法能模仿成员模板的行为,唉!如果成员模板(或语言中的其它一些特性)对你非常重要,告诉你的编译器提供商。越多的用户要求新的语言特性,提供商将越快地实现它们。 * 注1: 这主要是因为auto_ptr的标准长年来一直没有确定。其最终描述被采纳于1997年11月。其细节可参考本书的主页。注意,此处的auto_ptr版本在实现上比正式版本在具体细节上有小小的省略:实际上auto_ptr位于名字空间std中(见Item M35)并且其成员函数承诺不抛任何异常。 
|