Is this a missusing of the friend keyword ?
I know many uses of friend keyword usually are a sign of bad design, i've implemented a simple Doubly Linked List Class, and a LinkedListIterator to use with the LinkedList class.
For that i declared each one of them "friends" of the other.
Is there a "cleaner" way to implement an iterator ?
Code:
struct Node
{
Node(Entry val)
{
next = NULL;
previous = NULL;
this->val = val;
}
Node()
{
next = NULL;
previous = NULL;
}
Node *next;
Node *previous;
Entry val;
};
//! LinkedList
/*!
Basic implementation of a linked list
*/
class LinkedList
{
public:
int i;
friend class LinkedListIterator;
// constructors
LinkedList(void);
~LinkedList(void);
// metods
void Append(Entry val);
void Prepend(Entry val);
void RemoveHead();
void RemoveTail();
void RemoveNode(LinkedListIterator *it);
Entry Head()
{
assert(m_head);
return m_head->val;
}
private:
Node *m_head; // head of the linked list
Node *m_tail; // tail fo the linked list
LinkedList(const LinkedList &){}; // avoid user trying to pass by value
};
class LinkedListIterator
{
public:
friend class LinkedList;
// constructors
LinkedListIterator(const LinkedList *list)
{
m_list = list;
m_current = list->m_head;
}
~LinkedListIterator()
{
}
// methods
void Next()
{
m_current = m_current->next;
}
void Start()
{
m_current = m_list->m_head;
}
bool IsValid()
{
return m_current != NULL;
}
Entry * GetValue()
{
return &m_current->val;
}
private:
const LinkedList *m_list; // the list we're iterating
Node *m_current; // current node we're in
};