Struct inheritance - (strange?) problem.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
struct BasicNode
{
    QString     data;
    BasicNode*  first_child;

    BasicNode(QString _data): data (_data){
        this->first_child = NULL;
    }

    BasicNode(){
        this->data          = QString("Unnamed BasicNode instance");
        this->first_child   = NULL;
    }

    void setBasicOnFirstChild(QString _data){ this->first_child   = new BasicNode(_data);}
    void setCommonOnFirstChild(QString _data){ this->first_child   = new CommonNode(_data);}
};

struct CommonNode: public BasicNode
{
    BasicNode*  second_child;

    CommonNode(QString _data){
        this->data          =_data;
        this->first_child   = NULL;
        this->second_child  = NULL;
    }

    CommonNode(){
        this->data          = QString("Unnamed CommonNodeInstance");
        this->first_child   = NULL;
        this->second_child  = NULL;
    }

    void setBasicOnSecondChild(QString _data){this->second_child  = new BasicNode(_data);}
    void setCommonOnFirstChild(QString _data){this->first_child   = new CommonNode(_data);}
    void setCommonOnSecondChild(QString _data){this->first_child   = new CommonNode(_data);}
};


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.

Greetings,
Last edited on
Sorry, for double post. I just wanted to bump my post back to main page.
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...
closed account (zb0S216C)
What's your error (s)?

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).

Wazzak
Topic archived. No new replies allowed.