Hello, when I tried to write two simple structures: BasicNode which store pointer to only one child, and second called CommonNode which contain pointers to first child and second child. Relation between BasicNode and CommonNode is: struct CommonNode : public BasicNode
and when i tried to use construction like this in main file:
1 2 3 4 5
BasicNode* b = new Node("basic node");
b->setCommonOnFirstChild("common node");
b->first_child->second_child /* <- this is unnable, I could onlu use sth like below: */
b->first_child->first_child;
Here I put the most important part of code or this topic which contain implementation both of structures.
In fact, I can't even modified pointers in for example this method from CommonNode:
1 2 3 4 5 6 7 8
void setCommonOnSecondChild(QString _data){
this->first_child = new CommonNode(_data);
this->first_child->first_child = something ... // this one I can modiied
this->irst_child->second_child = something ... // but this one I can't
// because IDE said: secondChild is not member of BasicNode
}
I'll be very happy for any hints how could I resolve it. Maybe its a stupid error coz I started to learn C++ and Qt not so far and mayby it's my bad interpretation of inheritance, as I wrote I'm quite new at c++. But aren't fileds in structures public from default? They are, so I think the should be visible in CommonNode structure.
first_child is a pointer to BaseNode, so you can't access members declared in CommonNode. It's that simple. You can try downcasting, but that requires knowing you are in fact pointing to a CommonNode and not a BaseNode...
Note that this->data = QString("Unnamed CommonNodeInstance"); is bad. Why create a new object, assign it to data then destroy it? I would chance this as it's a performance issue (not by a large margin, but the decrease is there).