Suppose we have the simplest structure, classes Component, Leaf : Componentand Composite : Component. Each class object Leafhas int idone that gives it its identifier. A complex class will look like this:
class Composite : public Component
{
public:
void removeComponent(Component*);
private:
std::vector<Component*> coll;
};
And the sth like sheet class:
class Leaf : public Component
{
public:
//stuff
int getID();
private:
int id;
};
And the question is how to define a function removeComponent(Component* cmp). cmp is actually a Leaf, but I need to access the Componentvector coll, so it should be Component(I think). The removeComponent method takes an object Leafand removes all other Sheets with the same identifier from the entire structure.
Now I thought of two paths (none of which work: P):
First
void Composide::removeComponent(Component* cmp)
{
std::vector<Component*>::const_iterator it;
for(it = coll.begin(); it != coll.end(); ++it)
{
(*it)->removeComponent(cmp);
}
}
Second
void Composide::removeComponent(Component* cmp)
{
std::vector<Component*>::const_iterator it;
for(it = coll.begin(); it != coll.end(); ++it)
{
if((*it)->equals(*cmp))
it = erase(it);
}
}
, . , , , , Leaf, . , ?:)