Effective C++ 45. Use member function templates to accept "all compatible types."

I

class Top { };
class Middle: public Top{ };
class Bottom: public Middle { };
Top *pt1 = new Middle;
Top *pt2 = new Bottom;
const Top *pt2 = pt1;
tmplate<typename T>
class SmartPtr {
public:
explicit SmartPtr(T* realPtr);
};
SmartPtr<Top> pt1 = SmartPtr<Middle>(new Middle);

There is no inherent relationship among different instantiations of the same template, so compilers view SmartPtr<Middlet> and SmartPtr<Top> as completely different classes.

template<typename T>
class SmartPtr {
public:
    template<typename U>
    SmartPtr(const SmartPtr<U>& other);
};

we want to be able to create a SmartPtr<Top> from a SmartPtr<Bottom>, but we do not want to be able to create a SmartPtr<Bottom> from a SmartPtr<Top>, as that’s contray to the meaning of public inheritance.
I.I

template<typename T>
class SmartPtr{
public:
    template<typename U>
    SmartPtr(const SmartPtr<U>& other): heldPtr(other.get()) { }
    T* get() const { return heldPtr; };
private:
    T* heldPtr;
};

heldPtr(other.get()) this will compile only if there is an impilcit conversion from a U* pointer to a T* pointer, and that is precisely what we want.

II.
Declaring a generalized copy constrctor (a member template) in a class doesn’t keeep compilers from generating their own copy constructor, so if you want to control all aspects of copy construction, you must declare both a generalized copy constructor as well as the “normal” copy constructor.

template<typename T> clas shared_ptr {
public:

    // copy constructor
    shared_ptr(shared_ptr const& r);

    // generalized copy constructor
    template<typename Y>
        shared_ptr(shared_ptr<Y> const& r);


    // copy assignment 
    shared_ptr& operator=(shared_ptr const& r);
    // generalized copy assignment
    template<class Y>
        shared_ptr& operator = (shared_ptr<Y> const& r);
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值