Effective C++之45

    技术2022-05-11  62

    条款45:运用成员函数模板接受所有兼容类型        对shared_ptr<int>和shared_ptr<const int>来说,这是完全不同的两种类型。为了避免这样的可能带来的不便,需要在程序做兼容。        示例如下: template<class T> class shared_ptr { public:

           shared_ptr(shared_ptr const & r)//拷贝构造函数

           template<class Y>

    shared_ptr(shared_ptr<Y> const & r); //泛化拷贝构造函数

    }        使用成员函数模板可以生成接受所有兼容类型的函数,如果声明了泛化类型的函数,并不代表可以取代一般类型的函数。如果必要,还需要声明一般类型的函数。  

    最新回复(0)