C++ C++;不同类类型节点的树/林结构
\\在实现通过转换公共基类找到的解决方案时 \\有男性成员 \\我发现了一些关于univeral的参考资料,因为这是我提出的另一个问题: 另请参阅原问题 我想组织不同类型对象的层次树结构 这个解决方案应该在编译时完成它的工作,所以我发现我只能在没有任何强制转换的情况下使用模板来完成这项工作 经过一些尝试后,编辑日志中出现了一些基本缺陷 我找到了一种方法把它分成两个班C++ C++;不同类类型节点的树/林结构,c++,templates,tree,unique-ptr,type-safety,C++,Templates,Tree,Unique Ptr,Type Safety,\\在实现通过转换公共基类找到的解决方案时 \\有男性成员 \\我发现了一些关于univeral的参考资料,因为这是我提出的另一个问题: 另请参阅原问题 我想组织不同类型对象的层次树结构 这个解决方案应该在编译时完成它的工作,所以我发现我只能在没有任何强制转换的情况下使用模板来完成这项工作 经过一些尝试后,编辑日志中出现了一些基本缺陷 我找到了一种方法把它分成两个班 存储所有节点并在向量 节点存储T对象和与其他节点的关系的模板 我的想法还没有一种方法可以访问存储在node类中的对象的Mem
- 存储所有节点并在向量
- 节点存储T对象和与其他节点的关系的模板
class Forest
{
public:
template<typename T>
{friend class Node<T>;} \\every Node<T> should have access to the forest
Forest();
~Forest();
Forest(const Forest&)=delete; \\does not make any sense to copy or assign the forest to another forest.
Forest operator=(const Forest&)=delete;
int insertroot() \\every tree has a void nullptr seed/root so that the forest does not need to be templatet, returns the treenumber
{ \\implementation
if(this->Nodes.size()==0)
{
std::vector<std::unique_ptr<Node<void> > > h0;
h0.push_back(std::unique_ptr<Node<void>(new Node<void>(nullptr));
this->Nodes.push_back(h0);
}else
{
this->Nodes[0].push_back(std::unique_ptr<Node<void>(new Node<void>(nullptr,this)));
}
return this->Nodes[0].size()-1;
}
Node<void>* getroot(int i) \\ to later allow access to the children and the Objects inside them
{
if(Nodes.size>0){
if((i>0)&(i<Nodes[0].size()))
{
return Nodes[0][i].get();
}
}
private:
std::vector<std::vector<unique_ptr<Node<void> > > nodes; \\is it possible to fill this vector with any Node<T>? its a vector*2 to a unique_ptr to a classpointer with a member pointer to any class. from what i read about templates they create a extra class for every type, so basicly the unique_ptr have a different type and i cannot store different types in a vector without casting?
}
template<typename T>
class Node
{
public:
Node(T n,Forest * Fo) \\ every Node is in the forest and has access to the other nodes and forest information
:object(std::unique_ptr(n)),F(Fo)
{
if(n==nullptr)
{
this->lvl=0;
this->place=F->Node[0].size();
this->parent=-1;
}
}
~Node();
Node(const Node&)=delete;
Node operator=(const Node&)=delete;
T getObject(){return object.get();} \\how does the compiler know the type? see getchild
template<typename C>
{
Node<C> * getchild(int){} \\not yet exsisting implementation of get child[int] how do i teach the compiler what int responds to what type?
\\when i understand templates correct then Node<C> are different Classes for every C??
addChild(C c)
{
Node * n=new Node(c,this->F);
n->parent=this->place;
n->lvl=this->lvl+1
if(F->nodes.size()<=n->lvl)
{
n->place=0;
h0=std::vector<unique_ptr<Node<C>> >;
h0.push_back(unique_ptr<Node<C>(n))
F->Nodes.push_back(h0); \\are vector<uniptrNode<C> > and vector<uniptrNode<void>> compatible?
}else
{
n->place=F->nodes[n->lvl].size();
F->Nodes[n->lvl].push_back(unique_ptr<Node<C> >(n));
}
this->children.push_back(c->place);
}
}
private:
int parent,place,lvl;
std::vector<int> children;
unique_ptr<T> object;
Forest * F;
}
类林
{
公众:
模板
{friend class Node;}\\n每个节点都应该有权访问林
森林();
~Forest();
林(const-Forest&)=delete;\\将林复制或分配给另一个林没有任何意义。
林运算符=(常数林&)=删除;
int insertroot()\\每棵树都有一个空的nullptr seed/root,因此林不需要模板化,返回treenumber
{\\n实现
if(this->Nodes.size()==0)
{
std::载体h0;
h0.push_back(std::unique_ptrNodes.push_back(h0);
}否则
{
this->Nodes[0]。向后推(std::unique\u ptrNodes[0]。size()-1;
}
Node*getroot(int i)\\稍后允许访问子级及其内部对象
{
如果(节点大小>0){
如果((i>0)&(iplace=F->Node[0].size();
此->父项=-1;
}
}
~Node();
节点(const Node&)=删除;
节点运算符=(const Node&)=删除;
T getObject(){return object.get();}\\n编译器如何知道类型?请参阅getchild
模板
{
Node*getchild(int){}\\get child[int]的实现还不存在,我如何教编译器什么int响应什么类型?
\\当我理解模板正确时,每个C?的节点都是不同的类??
addChild(C)
{
Node*n=新节点(c,this->F);
n->parent=this->place;
n->lvl=此->lvl+1
if(F->nodes.size()lvl)
{
n->place=0;
h0=std::vector;
h0.push_-back(唯一的节点)。push_-back(h0);\\vector和vector兼容吗?
}否则
{
n->place=F->nodes[n->lvl].size();
F->Nodes[n->lvl]。向后推(唯一的\u ptr(n));
}
这->儿童。推回(c->位置);
}
}
私人:
int父级、位置、lvl;
性病媒儿童;
唯一的ptr对象;
森林*F;
}
有人知道实现这样一个容器的方法吗?
也许有一些抽象类型我没有发现,所以我可以添加一个方法类型getnodetype(int)或checknodetype(int,type),我可以用auto nodex=y->getObject()来分配它吗?但是编译器如何知道methodes nodex有什么和没有什么
编辑:我删除了原来的帖子,因为v4非常接近于一个工作解决方案版本1-3应该在editlog中。我想你需要类似于
boost的东西。任何或QVariant
你的析构函数都是不必要的。容器和智能指针的存在使得你不必手动删除东西。析构函数就是这样的e这样子对象在对象之前先被删除为什么这很重要?在任何情况下,子对象都会在对象之前被删除,因为它在类定义中出现在对象之后。如果您的类要求按特定顺序删除内容,您应该将其记录在注释中。为什么不使用模板…
?或使用T1
代替普通的T
?因为我对模板不是很熟悉,我不知道模板是否应该是公共成员函数的额外模板。这样,如果t1是相同的void或base,编译器会为addchild函数生成一个具有不同重载的类?或者std::Experiative::any
,voted从C++14输出到一个单独的TS,由一些编译器支持。