【发布时间】:2013-12-29 09:58:42
【问题描述】:
我有 2 个班级 classNode 和 classTree。
在我的 classTree 构造函数中,我创建了一个 vector<classNode> nodes 并推送
返回classNodes 对象。
然后我分配了一个 classNode* 指针到第一个成员
vector<classNode>:
this->root= &nodes[0];
现在退出构造函数后,我的指针显示垃圾,但我必须使用
它。我可以为此目的使用static vector<classNode> nodes 吗?或者是
有没有人给我建议?
编辑
struct nodeInfo{
string name;
int tab;
};
ClassTree(){
vector<nodeInfo> container;
vector<ClassNode> nodes;
//assume that container is a not-empty vector.I removed this part for simplicity.
for(int i=0;i<container.size();i++){
nodes.push_back(ClassNode (container[i].name,1));
}
this->root=&nodes[0];
}
ClassTree::function2(){
//now I want to use root here. I mean I want to reach &nodes[0] here.
}
【问题讨论】:
-
堆栈溢出经验法则 #3:“现在,众所周知”之后是一些完全的误解。
-
可以显示
classTree构造函数的代码吗? -
@KerrekSB - 以及
which is obviously true..和democratic republic of...等 -
@EdHeal:我的最爱:“我的程序运行良好。只有一件事:”(UB 紧随其后)
-
@RoeeGavirel 我添加了它