Effective C++ Item 45 ― 2009年08月31日 06時28分14秒
- メンバ関数テンプレートを用いて相互互換のある型の変換を実装する。
- メンバ関数テンプレートを用いてコピーコンストラクタや代入演算子をつくってたとしても、通常のコピーコンストラクタと代入演算子は必要になる。
スマートポインタはリソース管理の上では、普通のポインタに勝る。しかし、型の自動変換は普通のポインタの方が上手だ。スマートポインタを用いる場合は、変換関数を作らないといけない。
member template for a "generalized copy constructor" の例。
template<typename T> class SmartPtr { public: template<typename U> SmartPtr(const SmartPtr<U>& other); };
以下は上から順に、コピーコンストラクタ、generalized コピーコンストラクタ、代入演算子、generalized 代入演算子。
template<typename T> class shared_ptr { public: shared_ptr(shared_ptr const& r); template<class Y> shared_ptr(shared_ptr<Y> const& r); shared_ptr& operator=(shared_ptr const& r); template<class Y> shared_ptr& operator=(shared_ptr<Y> const& r); };
最近のコメント