多重继承和 templates

两者互补。多重继承欠缺技
术( mechanics), templates 有丰富的技术。多重继承缺乏型别信息,而那东西在templates
头大量存在。 Templates 的特化无法扩张( scales), 多重继承却很容易扩张。你只能为 template
成员函式写份预设版ᴀ,但你可以写数量无限的base classes
根据以分析 , 如果我们将 templates 和多重继承组合起来 将会产生非常弹性的装置 , (device),

应该很适合用来产生链接库的「设计元素」( design elements)。

template <class T>
struct OpNewCreator
{
static T* Create()
{
return new T;
}
};
template <class T>
struct MallocCreator
{
static T* Create()
{
void* buf = std::malloc(sizeof(T));
if (!buf) return 0;
return new(buf) T;
}
};
template <class T>
struct PrototypeCreator
{
PrototypeCreator(T* pObj = 0)
: pPrototype_(pObj)
{}
T* Create()
{
return pPrototype_ ? pPrototype_->Clone() : 0;
}
T* GetPrototype() { return pPrototype_; }
void SetPrototype(T* pObj) { pPrototype_ = pObj; }
private:
T* pPrototype_;
};


// Library code
template <class CreationPolicy>
class WidgetManager : public CreationPolicy
{ ... };

typedef WidgetManager< OpNewCreator<Widget> > MyWidgetMgr;

template <template <class Created> class CreationPolicy>
class WidgetManager : public CreationPolicy<Widget>
{ ... };

typedef WidgetManager<OpNewCreator> MyWidgetMgr;


template <template <class> class CreationPolicy>
class WidgetManager : public CreationPolicy<Widget>
{
...
void DoSomething()
{
Gadget* pW = CreationPolicy<Gadget>().Create();
...
}
};

template <template <class> class CreationPolicy = OpNewCreator>

class WidgetManager ...

policy 的确能够带给 WidgetManager 非常大的弹性。 第, 当你准备具现化 ( instantiating
WidgetManager 时,你可以从外部变更 policies,就和改变 template 自变量样简单。第,你
可以根据程序的特殊需求,提供自己的 policies。你可以采用 new malloc prototypes
个专用于你自己系统的罕见内存配置器。 WidgetManager 就像个小型的「程序代码生成
引擎」( code generation engine), 你可以自行设定程序代码的产生方式。
为了让应用程序开发员的日子更轻松 WidgetManager , 的作者应该定义些常用的 policies
并且以「 template 预设自变量」的型式提供最常用的 policy
template <template <class> class CreationPolicy = OpNewCreator>
class WidgetManager ...
注意, policies 和虚拟函式有很大不同。虽然虚拟函式也提供类似效果: class 作者以基ᴀ的
primitive)虚拟函式来建立高端功能,并允许使用者覆写这些基ᴀ虚拟函式的行为。然而如
前所示, policies 因为有丰富的型别信息及静态连结等特性,所以是建立「设计元素」时的ᴀ
质性东西。不正是「设计」指定了「执行前型别如何互相作用、你能够做什么、不能够做什么」
的完整规则吗? Policies 可以让你在型别安全( typesafe)的前提藉由组合各个简单的需求来
产出你的设计。此外,由于编译期才将 host class 和其 policies 结合在起,所以和手工打造的
程序比较起来更加牢固并且更有效率。
当然,也由于 policies 的特质,它们不适用于动态连结和进位接口,所以ᴀ质policies
传统接口并不互相竞争



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值