【发布时间】:2015-05-02 10:33:07
【问题描述】:
问题是:定义动作对象的容器(可能是模板)(将被命名为 Runner)。动作对象 (AO) 是执行用户提供的功能的对象。这个想法是,一旦动作容器加载了 AO,就可以要求它运行或执行它们。执行结果在 Results 数组中返回。示例:
AO a1(mul, x, x); //if run method is called it returns x*x
AO a2(add, y, 1); //if run method is called it returns y+1
AO a3(print, s); //if run method is called it prints s on cout
Runner<AO> r = {a1,a2,a3};
所以现在在运行器类中我应该如何实现一个构造函数,它接受常量数组并知道它的大小
template<typename a>
class runner{
a* m;
int size;
public:
runner(const a x[]){
//how to know the size of x[] to initialize m=new a[size] and then fill it with a1,a2,a3 ??
}
【问题讨论】:
-
Read about array templates。或者将大小作为参数传递。
标签: c++ arrays class initialization