C .当T类需要A类模板参数时,如何定义A类T型的模板参数
C++. How to define template parameter of type T for class A when class T needs a type A template parameter?
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;
}
编辑* ** * ** ** ** ** ** * ** * ** * ** * ** ** *** *** *** ***
一点澄清。执行人是提供服务的单身人士。所有算法对象都需要执行器的服务。执行人有时会生成需要发送到特定算法对象的报告。他们通过处理器将其发送到正确的算法。
基本问题是定义执行程序需要算法,并且需要执行器来定义算法。
尝试重现代码,而不太确定您要实现的目标。对于初学者来说,这就是我对其进行了修改:
#include <string> //Added
#include <map> //Added
using namespace std;//Added
template <class T>
class Processor {
map<string,T> ts;
void Process(string str, int i) {
ts[str].Do(i);
}
};
template <class P>
class Executor {
Processor<P> &p; //Was Proc ???
Executor(P &p) : Processor<P>(p) {} //Was Proc ???
void Foo(string str, int i) {
p.Process(str,i);
}
void Execute(string str){} //Added return type void
};
template <class E>
class Algo {
public: //Added
static E e;
void Do(int i) {}
};
main () {
typedef Processor< Algo<int> > PALGO; //Added template argument to Algo
typedef Executor<PALGO> EPALGO;
typedef Algo<EPALGO> AEPALGO;
Executor<PALGO> executor(PALGO());
AEPALGO::e = executor;
}
在执行程序定义中修改了Proc到处理器 - (什么是proc?),并在Typedef处理器> PALGO中给出了模板参数;然后aepago :: e->那就是模板参数,而不是类Algo成员 - So Aepago :: e。现在,您将获得一个更易于管理的错误。它需要一个复制构造函数来转换类型。
您可以使用继承。
class X : public Executor<Processor<Algo<X>>> {};
其他,这是不可能的。
afaics,您不能使用 same Executor
type做到这一点。否则您将不得不定义
Executor<Processor<Algo<Executor<Processor<Algo<...> > > > > >
如果您用其他类型定义它,它可能会起作用,只要技术上有任何有意义的技术
class X {
...
};
Executor<Processor<Algo<Executor<Processor<Algo<X> > > > > >
或使用typedef
class X {...};
typedef Processor<Algo<X> > PALGO;
typedef Executor<PALGO> EPALGO;
typedef Algo<EPALGO> AEPALGO;
Executor<PALGO> executor(PALGO());
由于执行程序是单身人士,因此您可以将其定义移出其自己的Singleton类或executor中的算法。然后使所有需要了解执行器模板成员函数的算法函数。
template <class P>
class Executor {
static Executor e;
P &p;
Executor(P &p) : Proc(p) {}
void Bar(string str, int i) {
p.Process(str,i);
}
Execute(string str)
{
}
public:
static Executor& getE(){ return e;}
}
class Algo
{
void Do(int i) {}
template <class E>
void Foo()
{
E::getE().Execute("xxx");
}
}
解决!看评论。//* ***
#include <string>
#include <map>
#include <iostream>
using namespace std;
template <class T>
class Processor {
public:
map<string,T*> ts;
void Process(string str, int i)
{
ts[str]->Do(i);
}
};
template <class P>
class Executor {
public:
P &p;
Executor(P &inp) : p(inp) {}
void Bar(string str, int i) {
p.Process(str,i);
}
void Execute(string str)
{
cout << " Executor::Execute " << str << endl;
}
};
template <template <class> class E> //**********
class Algo
{
string str;
public:
Algo(const string &s) : str(s) {}
static E<Processor<Algo>> *e; //**********
void Do(int i) { cout << str << "::Do(" << i <<")"<< endl; }
void Foo()
{
e->Execute(str);
}
};
template <template <class> class E>
E< Processor<Algo<E> > >* Algo<E>::e; //**********
int main(int argc, char **argv)
{
typedef Algo<Executor> EALGO;
typedef Processor<EALGO> PALGO;
typedef Executor<PALGO> EPALGO;
PALGO p;
EPALGO executor(p);
EALGO::e = &executor; //**********
EALGO ealgo1("algo1"), ealgo2("algo2");
p.ts["algo1"] = &ealgo1;
p.ts["algo2"] = &ealgo2;
ealgo1.Foo();
ealgo2.Foo();
executor.Bar("algo1",1111);
executor.Bar("algo2",2222);
}
- 如何使用宏根据参数定义不同的数据类型?
- 有没有办法根据命令行参数定义数组大小?运行时与编译时实例化?
- 为什么我需要将默认引用参数定义为 const 以便为其分配一个左值?
- 如何通过opencv中的程序参数定义特征类型?
- 确定是否有针对特定类型的参数定义的超载函数
- C 试图使用函数参数定义2D数组
- 是否可以为模板模板参数定义别名
- C++11:用容器参数定义函数(类似于基于范围的)
- C++-用类参数定义矢量大小
- 使用void* 参数定义纯虚函数的抽象基类.派生类匹配参数是指向某种类型的指针
- 如果我使用非常量参数定义那些,我是否仍然会获得默认的复制构造函数和运算符 =
- 使用参数定义的打印方法名称
- 如何根据类参数定义成员类运算符
- 如何为引用参数定义模板函数,为指针参数定义相同的函数
- boost::函数参数定义不起作用
- 如何基于不同的参数定义相同的宏函数
- 如何基于不同的参数定义相同的宏函数
- 将参数定义为未指定的模板类型
- C++模板,vector.size用于默认参数定义
- 如何为CamShift的矩形输入参数定义矩形变量