【发布时间】:2012-12-19 22:28:00
【问题描述】:
Executor 类具有 P 类型的模板,它在构造函数中接受一个 P 对象。 Algo 类有一个模板 E,也有一个 E 类型的静态变量。处理器类有模板 T 和一个 Ts 的集合。
问题我如何定义 Executor< Processor<Algo> > 和 Algo<Executor> ?这可能吗?我看不出有什么办法来定义它,它是一种“无限递归模板参数”
查看代码。
template <class T>
class Processor {
map<string,T> ts;
void Process(string str, int i)
{
ts[str].Do(i);
}
}
template <class P>
class Executor {
P &p;
Executor(P &inp) : p(inp) {}
void Bar(string str, int i) {
p.Process(str,i);
}
Execute(string str)
{
}
}
template <class E>
class Algo
{
static E e;
void Do(int i) {}
void Foo()
{
e.Execute("xxx");
}
}
main ()
{
typedef Processor<Algo> PALGO; // invalid
typedef Executor<PALGO> EPALGO;
typedef Algo<EPALGO> AEPALGO;
Executor<PALGO> executor(PALGO());
AEPALGO::E = executor;
}
编辑 ************ ****************强>
稍微澄清一下。 Executor 是一个提供服务的单例。所有的 Algo 对象都需要 Executor 的服务。 Executor 有时会生成需要发送到特定 Algo 对象的报告。它们通过处理器被发送到正确的算法。
基本问题是需要 Algo 来定义 Executor 并且需要 Executor 来定义 Algo。
【问题讨论】:
-
在 Algo 类中如何使用 e?为什么是静态的?
-
编辑代码:添加了 e
-
好的,但是现在谁调用 Algo:Foo?我的问题是,为什么 E 需要成为 Algo 的一部分?
-
所有算法对象都使用 E 作为服务。 Algo::Foo 被外部调用。
标签: c++ templates design-patterns template-meta-programming