I've tried to implement an easy Node Class but when i try to access the child node via the root node its empty, but if i access the child node directly it's filled correctly. Do i have somewhere a logic mistake or can some one give me a hint whats wrong?
Node::Node(QString name, Node *parent)
{
this->name = name;
this->parent = parent;
parent->children.append(this);
}
\\
class Node
{
public:
Node(QString name) { this->name = name; }
Node(QString name, Node *parent);
~Node(void);
void addChild(Node *child);
QString getName() { return name; }
Node* getChild(int row) { return children[row]; }
Node* getParent() { return parent; }
int childCount() { return children.size(); }
int getRow() {return this->parent->children.indexOf(this);}
QString log(int tabLevel = -1);
private:
QString name;
QList<Node*> children;
Node *parent;
};
I tried to find the mistake and my result is, that the child node seems to have two different addresses, so there are two different objects but i don't know why :/
Node rootNode = Node("rootNode");
Node childNode0 = Node("childNode0", &rootNode);
Node childNode1 = Node("childNode1", &rootNode);
Node childNode2 = Node("childNode2", &rootNode);
Node childNode3 = Node("childNode3", &childNode0);
Node childNode4 = Node("childNode4", &childNode0);
qDebug() << "RootNode: " + rootNode.getName() << " | RootChilds: " << rootNode.childCount();
qDebug() << "NodeName: " +rootNode.getChild(0)->getName() << " | NodeChilds: " << rootNode.getChild(0)->childCount();
for(int i = 0; i < childNode0.childCount(); i++)
{
qDebug() << "NodeName: " << childNode0.getName() << " | NodeChilds: " << childNode0.childCount() << "Child Nr: " << i << " Name -> " << childNode0.getChild(i)->getName();
}
qDebug() << "Adress via root: " << rootNode.getChild(0) << "\nAdress via node: " << &childNode0 ;
}
That outputs:
"RootNode: rootNode" | RootChilds: 3
"NodeName: childNode0" | NodeChilds: 0
NodeName: "childNode0" | NodeChilds: 2 Child Nr: 0 Name -> "childNode3"
NodeName: "childNode0" | NodeChilds: 2 Child Nr: 1 Name -> "childNode4"
Adress via root: 0x41fc84
Adress via node: 0x41fcc0
I hope someone can help me
Regards