类初始化如何设置儿子的父指针?
一个类如下,初始化时,如何设置儿子的父节点指针?
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; }}
vector<Node*> children;
[解决办法]
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; }};