条款45: 运用成员函数模板接受所有兼容类型

真实指针做得很好的一件事是,支持隐式转换(implicit conversions)。

class Top {...}
class Middle: public Top {...}
class Bottom:public Middle {...}
Top* pt1 = new Middle;    //将Middle*转换为Top*
Top* pt2 = new Bottom;    //将Bottom*转换为Top*
const Top* pct2 = pt1;      //将Top*转换为const Top*

Templates和泛型编程(Generic Programming)

但是,如果以带有base-derived关系的B,D两个类型分别具现化某个template,产生出来的两个具现体并不带有base-derived关系。

#include<iostream>
#include<string>
using namespace std;

template<class T>
class SmartPtr
{
public:
    explicit SmartPtr(T* realPtr = NULL)
            : pointee(realPtr)
    {
    }

    T* operator->() const
    {
        return pointee;
    }

    T& operator*() const
    {
        return *pointee;
    }
private:
    T* pointee;
};

struct MusicProduct
{
    MusicProduct(string title)
    {
        this->title = title;
    }
    virtual void displayTitle() = 0;
    virtual ~MusicProduct()
    {
    }

    string title;
};

struct CD : MusicProduct
{
    CD(string title) : MusicProduct(title)
    {
    }
    void displayTitle()
    {
        cout << title << endl;
    }
};

struct MP3 : MusicProduct
{
    MP3(string title) : MusicProduct(title)
    {
    }
    void displayTitle()
    {
        cout << title << endl;
    }
};

void displayAndPlay(SmartPtr<MusicProduct>& pmp)
{
    pmp->displayTitle();
}

int main(int argc, char **argv)
{
    SmartPtr<CD> cdMusic(new CD("BEYOND LIVE CD"));
    SmartPtr<MP3> mp3Music(new MP3("BEYOND MP3"));

    displayAndPlay(cdMusic);
    displayAndPlay(mp3Music);

    return 0;
}

error: invalid initialization of reference of type ‘SmartPtr<MusicProduct>&’ from expression of type ‘SmartPtr<CD>’
displayAndPlay(cdMusic);

我们可以在前例SmartPtr的定义中增加成员函数模板(member function templates)实现代码,之后就可以正常编译和运行了。

// Member Function Templates
template<class newType>
operator SmartPtr<newType>()
{
    return SmartPtr<newType>(pointee);
}

看起来很神奇吧,看看《More Effective C++》对此的解释:现在请你注意,这可不是魔术——不过也很接近于魔术。假设编译器有一个指向T对象的智能指针,它要把这个对象转换成指向“T的基类”的智能指针。编译器首先检查 SmartPtr的类定义,看其有没有声明明确的类型转换符,但是它没有声明。编译器然后检查是否存在一个成员函数模板,并可以被实例化成它所期望的类型转换。它发现了一个这样的模板(带有形式类型参数 newType),所以它把newType绑定成T的基类类型来实例化模板。

当然,成员函数模板还有另外一种实现方式:

// Member Function Templates
template<class U>
SmartPtr(const SmartPtr<U>& other) : pointee(other.pointee)
{
}

这段代码的意思是,对任何类型T和任何类型U,这里可以根据SmartPtr<U>生成一个SmartPtr<T>,因为Smart<T>有个构造函数接受一个SmartPtr<U>参数。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容