我不知道为什么你要添加的Inner
类型模板参数,因为你定义holder
是一种类型的基于Container
和inner
,这两者都可以在您声明持有者的地方提供。
您打算使用struct inner
以外的任何其他类型作为模板参数吗?Container
?如果没有,下面的简化代码编译和VS2010跑了我:
#include <vector>
#include <stdio.h>
template <typename C>
struct inner{
C * objects[16];
bool hasobj;
inner():hasobj(false){}
};
template <typename T>
class Container {
inner<Container> holder;
T value;
public:
Container(const T& valueP){
value = valueP;
}
void AddChild(Container* rhs){
holder.objects[0] = rhs; //Always using first location, just for example
holder.hasobj = true;
}
void PrintStuff()const{
if(holder.hasobj){
holder.objects[0]->PrintStuff();
}
printf("VAL IS %d\n", value);
}
};
int main(){
Container<int> c(10);
Container<int> c1(20);
c1.AddChild(&c);
c1.PrintStuff();
}
基本上,这是假设container
总是在inner
来定义holder
,这有助于摆脱多余的模板参数,当定义Container
。希望这有助于:) 阿伦
有一种叫做“好奇递归模板模式”(CRTP)的东西?也许它会帮助,不确定... http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern – Jimbo
这种递归数据类型将有哪些新行为将它与链接列表区分开来? – abiessu
@abiessu,我目前的用途实际上是基数树。每个节点都有一个如何到达下一个节点的索引,“内部”是指定该索引的策略。也就是说,模板允许说明孩子是如何管理的,而不是固定的红黑,散列或其他策略。 –