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