468,503 Members | 2,174 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,503 developers. It's quick & easy.

Implementation of iterator in Composite Pattern

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;
}
Jan 18 '08 #1
1 3447
On Jan 18, 7:48 am, mathieu <mathieu.malate...@gmail.comwrote:
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<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;

}
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
*/
Jan 18 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Martin Smith | last post: by
2 posts views Thread by Da Wang | last post: by
7 posts views Thread by desktop | last post: by
8 posts views Thread by =?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?= | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.