Implementation of iterator in Composite Pattern

M

mathieu

Hi there,

I am currently stuck on the following simple problem: implementing
an iterator for a class using the Composite Pattern. I feel like I
have to reimplement a complete new class for iterating over the
Element, since I cannot use directly the std::vector<>::iterator. Did
I miss anything, can I reuse something from the STL ?

Thanks
-Mathieu

class Element
{
public:
Element():key(-1) {}
virtual ~Element() {}
int getkey() const { return key; }
void setkey(int k) { key = k; }
private:
int key;
};

class CompositeElement : public Element
{
public:
CompositeElement():Elements() { }
typedef std::vector<Element*>::iterator iterator;
iterator begin() { return Elements.begin(); }
iterator end() { return Elements.end(); }
void push_back(Element * el)
{
Elements.push_back(el);
}

private:
std::vector<Element*> Elements;
};

int main()
{
CompositeElement root;
// fill root ...

for (CompositeElement::iterator it = root.begin(); it != root.end();
it++)
{
Element* el = *it;
std::cout << el->getkey() << std::endl;
}

return 0;
}
 
S

Salt_Peter

Hi there,

I am currently stuck on the following simple problem: implementing
an iterator for a class using the Composite Pattern. I feel like I
have to reimplement a complete new class for iterating over the
Element, since I cannot use directly the std::vector<>::iterator. Did
I miss anything, can I reuse something from the STL ?

Thanks
-Mathieu

class Element
{
public:
Element():key(-1) {}
virtual ~Element() {}
int getkey() const { return key; }
void setkey(int k) { key = k; }
private:
int key;

};

class CompositeElement : public Element
{
public:
CompositeElement():Elements() { }
typedef std::vector<Element*>::iterator iterator;

typedef typename std::vector said:
iterator begin() { return Elements.begin(); }
iterator end() { return Elements.end(); }
void push_back(Element * el)
{
Elements.push_back(el);
}

private:
std::vector<Element*> Elements;

};

int main()
{
CompositeElement root;
// fill root ...

for (CompositeElement::iterator it = root.begin(); it != root.end();
it++)
{
Element* el = *it;
std::cout << el->getkey() << std::endl;
}

return 0;

}

I'm not convinced that a CompositeElement is_an Element. Rather, i see
an abstract type from which both an Element and a Composite might be
classified by.

#include <iostream>
#include <vector>

struct Component
{
virtual ~Component() = 0;
};

Component::~Component() { }

template< typename K >
class Element : public Component
{
K key;
public:
Element() : key(-1) {}
Element(const K k) : key(k) {}
int getkey() const { return key; }
};

template< typename T >
class Composite : public Component
{
std::vector< T > vt;
public:
Composite() : vt() { }
typedef typename std::vector< T >::iterator iterator;
iterator begin() { return vt.begin(); }
iterator end() { return vt.end(); }
void push_back(const T& p)
{
vt.push_back(p);
}
};

int main()
{
typedef Composite< Element<int>* > CompositeElementInt;
CompositeElementInt root;

Element< int > e;
root.push_back(&e);
Element< int > e9(9);
root.push_back(&e9);

typedef CompositeElementInt::iterator CIter;
for ( CIter it = root.begin();
it != root.end();
++it )
{
std::cout << (*it)->getkey() << std::endl;
}
}

/*
-1
9
*/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top