Problem with inheritence

I have a problem with inheritence, I will try to explain it.

I have a treeview which I would like to populate, I use Qt for the gui elements but this problem isn't Qt related. It is going to be a treeview with a headerbar en below that one level that describes a certain group with its own information and it has child items to have some other information.

To create this I tried to make one abstract class named AbstractPostItem, this class als functions as the root item of the treeview.

For the description items in the treeview I created PostHeaderItem which inherits AbstractPostItem and for the child items of PostHeaderItem I created PostDataItem which also inherits AbstractPostItem.

Now from a dialog I try to create a new PostHeaderItem with its children, for that I use the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
PostHeaderItem::PostHeaderItem(QStringList files, QString subject, QString group)
{
    foreach(QString i, files)
    {
        PostDataItem *newItem = new PostDataItem(i);
        newItem->setSubject(subject);
        newItem->setGroup(group);
        newItem->setParent(this);
        addItem(newItem);
    }
    setSubject(subject);
    setGroup(group);
}


Which is called from:

1
2
3
4
5
AbstractPostItem *NewPost::getPost()
{
    PostHeaderItem *newItem = new PostHeaderItem(files, ui->inputSubject->text(), ui->inputUsenetGroup->text());
    return newItem;
}


Which is called from:

1
2
3
4
5
6
7
8
9
10
11
12
13
void PostView::newPost()
{
    AbstractPostItem *tmpAbstractPostItem;
    postScreen = new NewPost(this);
    postScreen->setWindowFlags(Qt::Window);
    postScreen->setAttribute(Qt::WA_ShowModal);
    if(postScreen->exec() == QDialog::Accepted)
    {
        tmpAbstractPostItem = postScreen->getPost();
        postsList->addPost(tmpAbstractPostItem);
        postScreen->close();
    }
}


Now my problem is that in the first and second code bit everything works just as expected, but when I try to call functions from tmpAbstractPostItem in the third code bit it uses the functions in the AbstractPostItem class instead of the PostHeaderItem class which is what I want.

So my question is how can I accomplish this?
closed account (1yR4jE8b)
Are these functions you are calling virtual in the base class?
Yes they are all virtual, but not pure virtual and all of them have an implementation in the base class.

For example I have a childCount function that is declared like this in AbstractPostItem:
virtual int childCount() { return childItems.count(); };

And declared like this in PostHeaderItem:
int childCount() const { return childItems.count();};

Now when I call this function from either of the two first code bits in my previous post the function in PostHeaderItem is executed but when I call it from the third code bit the function from AbstractPostItem is executed
Last edited on
closed account (1yR4jE8b)
I think you might also be able to go tmpAbstractPostItem->AbstractPostItem::function_you_want() but I'm not sure.
Last edited on
The problem was that as you can see in the above example that in PostHeaderItem the function is a const function but that wasn't the case in AbstractPostItem. When I changed that it called the correct function.
closed account (1yR4jE8b)
ah, you added in those functions while I was doing up my post.
I also only noticed it when I wrote it there and saw the difference. But thanks for the help.
Topic archived. No new replies allowed.