类初始化如何设置儿子的父指针?
一个类如下,初始化时,如何设置儿子的父节点指针?
- C/C++ code
class Node{ Node *parent; vector<Node> children; Node(void) { this->parent = 0; for (int i=0;i<4;++i) this->children.push_back(Node(this)); ////这个this无法传递给儿子,这种该怎么初始化呢? } Node(Node *p) { this->parent = p; }}
在push的时候,儿子节点无法获得父节点的指针,就无法设置所有儿子(这里有4个)的parent指针?
不知道xml或者html的类是怎么实现的?因为需要每一个Node都能获得其parent节点才行啊
[解决办法]
只能使用
- C/C++ code
vector<Node*> children;
[解决办法]
- C/C++ code
class Node{ Node *parent; vector < Node* > children; public: Node(void) { this->parent = 0; for (int i=0;i<4;++i) this->children.push_back(new Node(this)); ////这个this无法传递给儿子,这种该怎么初始化呢? } Node(Node *p) { this->parent = p; }};