![]() |
#2
rjsp2017-06-26 08:29
|

template<typename Tp> class shared_ptr {
public:
template<typename _Tp1>
explicit shared_ptr(_Tp1* __p) //(b) // 这里为什么是模板函数
: __shared_ptr<_Tp>(__p) { }
};
//下面的写法不行么
template<typename Tp> class shared_ptr {
public:
explicit shared_ptr(_Tp* __p) //(a) 这样不行么
: __shared_ptr<_Tp>(__p) { }
};
public:
template<typename _Tp1>
explicit shared_ptr(_Tp1* __p) //(b) // 这里为什么是模板函数
: __shared_ptr<_Tp>(__p) { }
};
//下面的写法不行么
template<typename Tp> class shared_ptr {
public:
explicit shared_ptr(_Tp* __p) //(a) 这样不行么
: __shared_ptr<_Tp>(__p) { }
};
在处理动态绑定问题时
class B {};
class D : public B {};
shared_ptr<B> sp(new D()); //会调用对应的模板构找函数, 但是为什么是模板函数呢, (a) 就可以实现功能啊
这里有什么优缺点么?