多重继承和 templates

来源:互联网 发布:java进程同步编程 编辑:程序博客网 时间:2024/05/16 13:54
两者互补。多重继承欠缺技
术( 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
原创粉丝点击