我的任务是创建一个像标准库一样的类List
。我无法让迭代器正常工作,因为它必须在从末尾递减时访问链表的尾部。这是我的头文件的一部分:
typedef int T;//for now; eventually will be templated
class list;//**forward declaration, doesn't let other classes know about _tail.**
class Node
{
//this works fine; class definition removed to make post shorter
};
class list_iterator
{
private:
Node* _node;
list* _list;
public:
//constructor
list_iterator& operator--(){_node=_node?(_node->_prev):(_list->_tail);return *this;}
//some other declarations
};
class list
{
friend class list_iterator;
private:
Node/*<T>*/ *_head,***_tail**;
int _size;
public:
typedef list_iterator iterator;
//some constructors and other method declarations
iterator begin() const {iterator it(_head);return it;}
iterator end() const {iterator it(0);return it;}
//more method declarations
};
我试图将重要部分加粗,但它只是用星号包围它们。注意:大部分成员函数都定义在 cpp 文件中;他们都碰巧被删除了一个简短的帖子。