【发布时间】:2012-07-17 16:20:57
【问题描述】:
我的任务是创建一个类似于标准库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 文件中;他们都碰巧因为一个简短的帖子而被删除。
【问题讨论】:
-
你在这里给出的函数实现叫做inline。您应该了解这对编译器意味着什么,以便您可以继续正确使用它。
-
至于粗体格式...看起来你放了太多星号。 Bold 只需要其中两个。如果这不起作用,请尝试在前两个星号之前和最后两个星号之后添加空格。 (事实上,您的代码格式通常可以通过一些适当的空格来改进。)
-
我想强调其中一个指针。原来这个功能被拒绝了(在代码中转义粗体) - meta.stackexchange.com/questions/32705/bold-code-in-a-question
-
抱歉,我遇到了解析错误。我错过了额外的 * 用于指针声明。另外,感谢您的链接。
标签: c++ class iterator datamember