[Solved]Inheritance problem

Hi,

I have derived a class but can't use the base class method from a child object.
Here is the simplified definition of the two classes :

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
  class Base
  {
    public:
      Base();
      ~Base();
      
      // A lot of methods

      void do();

    private:
      std::vector<aStructure*> container;
  };

  class Derived : public Base
  {
    public:
      Derived();
      ~Derived();

      void preDo();

    private:
      std::vector<aStructure*> container;
  };


I need to use the do() method from a Derived object. However, when I call do() from the child object, I simply get no result.

1
2
3
4
5
6
7
  Derived* der = new Derived();
  der->preDo();
  der->do();  // Do nothing.
  
  // But ...
  Base* bas = new Base(der->getContainer());
  bas->do(); // Expected result. 


Can anyone help me please ?

Thank you in advance.
Last edited on
Why do have a container member in in the derived class as well as the base class? Do you also reimplement getContainer()?

Without knowing what Derived does with its container and what Base does with its (other) container I can't say anything more.

Andy
Last edited on
Thank you for your response.

getContainer is just a getter I added to the derived class but not needed in the base class.

"container" appears in both classes because the do method work on it. The base class wasn't designed to be inherited but I realised that I needed some functionalities of this class.

Edit : The do method of the base class is convertTreeToQTreeWidget(QTreeWidget* parent); which use the two method listed below
1
2
3
  void convertTreeToQTreeWidget(QTreeWidget* parent);
  void convertTreeRepertoriesToQTreeWidgetItem(QTreeWidgetItem* parentItem, Repertory_t* parentRepertory);
  void convertTreeFilesToQTreeWidgetItem(QTreeWidgetItem* parentItem, Repertory_t* parentRepertory);


They are implemented as shown below :

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
void Tree::convertTreeToQTreeWidget(QTreeWidget* parent)
{
    // Répertoires top level.
    std::vector<Repertory_t*> rep = this->getRepertoryByLevel(0);

    QStringList* list = new QStringList();

    // Conversion des répertoires top level.
    for(unsigned int i = 0; i < rep.size(); i++)
    {
        list->append(QString::fromStdString(rep.at(i)->key));
        list->append("");
        list->append("");
        list->append(QString::number(this->dirCount(rep.at(i)->key)));
        list->append(QString::number(this->filCount(rep.at(i)->key)));

        QTreeWidgetItem* topLevel = new QTreeWidgetItem(parent, *list);

        this->convertTreeFilesToQTreeWidgetItem(topLevel, rep.at(i));

        this->convertTreeRepertoriesToQTreeWidgetItem(topLevel, rep.at(i));
    }
}

void Tree::convertTreeRepertoriesToQTreeWidgetItem(QTreeWidgetItem* parentItem, Repertory_t* parentRepertory)
{
    for(unsigned int i = 0; i < parentRepertory->successors.size(); i++)
    {
        Repertory_t* childRep = parentRepertory->successors.at(i);

        QStringList* list = new QStringList();
        list->append(QString::fromStdString(childRep->key));
        list->append("");
        list->append("");
        list->append(QString::number(this->dirCount(childRep->key)));
        list->append(QString::number(this->filCount(childRep->key)));

        QTreeWidgetItem* childItem = new QTreeWidgetItem(parentItem, *list);

        // Nom des fichiers.
        this->convertTreeFilesToQTreeWidgetItem(childItem, childRep);

        // Récursivité.
        this->convertTreeRepertoriesToQTreeWidgetItem(childItem, childRep);
    }
}

void Tree::convertTreeFilesToQTreeWidgetItem(QTreeWidgetItem* parentItem, Repertory_t* parentRepertory)
{
    for(unsigned int i = 0; i < parentRepertory->files.size(); i++)
    {
        QStringList* list = new QStringList();
        list->append(QString::fromStdString(parentRepertory->files.at(i)->name));
        list->append(QString::number(parentRepertory->files.at(i)->compSize));
        list->append(QString::number(parentRepertory->files.at(i)->uncompSize));

        QTreeWidgetItem* childItem = new QTreeWidgetItem(parentItem, *list);
    }
}


The getRepertoryByLevel function return a pointer to one of the item stored in the container and is implemented as shown below :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  Repertory_t* Tree::getRepertory(std::string key)
{
    std::vector<Repertory_t*>::iterator it;

    it = std::find_if(this->repertories.begin(), this->repertories.end(), Repertory_t(key));

    if(it != this->repertories.end())
    {
        return *it;
    }
    else
    {
        qDebug() << "Repertory " << QString::fromStdString(key) << " not found !";

        return NULL;
    }
}


The others structures of the container are accessed by the "successors" attribute of the structure returned by getRepertoryByLevel.

The derived class need to do the same treatment.
Last edited on
I solved the problem by turning the container attribute into a protected member in the base class and remove it into the derived class.
Sorry, that code is bit too involved for me to dig into.

But I don't think the derived class should be providing a container. do() is probably just working on the Base container and ignoring the Derived one.

If there also a Base constructor that takes a container it would explain why this line helps

Base* bas = new Base(der->getContainer());

as getContainer() returns the populated Derived container to the constructor which uses it to the new Base object's container.

You really need to remove getContainer() and the container from Derived and add a public getContainer() method to Base and modify Derived to work with the container returned by getContainer().

Andy
I don't think the derived class should be providing a container. do() is probably just working on the Base container and ignoring the Derived one.


Yes, I thought it was not the case.

Thank you for your attention.
Topic archived. No new replies allowed.