template<typename wide, typename narraw>
class wrap : public narraw
{
public:
virtual wide& toWide() = 0;
virtual const wide& toWide() const = 0;
protected:
~warp(){}
};
template<typename wide, typename narraw>
class rich : public wide, wrap<wide, narraw>
{
virtual wide& toWide() { return *this; }
virtual const wide& toWide() const { return *this; }
narraw& toNarraw() { return *this; }
const narraw& toNarraw() const { return *this; }
};
class wrap : public narraw
{
public:
virtual wide& toWide() = 0;
virtual const wide& toWide() const = 0;
protected:
~warp(){}
};
template<typename wide, typename narraw>
class rich : public wide, wrap<wide, narraw>
{
virtual wide& toWide() { return *this; }
virtual const wide& toWide() const { return *this; }
narraw& toNarraw() { return *this; }
const narraw& toNarraw() const { return *this; }
};

本文介绍了一个使用C++模板类实现的继承示例。该示例定义了两个模板类`wrap`和`rich`,其中`rich`类继承自`wrap`并实现了虚拟函数`toWide`和额外的`toNarraw`方法。
7990

被折叠的 条评论
为什么被折叠?



