0
我有四个班,其中两个迭代器:我的const和非const链接列表迭代器有什么不同?
LinkedList<T> -- holds the pointer to dummy node
LinkedListNode<T> -- Node with next and previous pointers and data T
LinkedListIterator<T> -- holds a pointer to LinkedListNode
LinkedListConstIterator<T> -- holds a pointer to LinkedListNode
他们俩只需要实现前向迭代器的功能。
在这种情况下,我的const迭代器中的函数会有什么不同?这是否意味着当我返回一个指针或任何给用户时,我应该把它设置为const?
理想情况下,你的'LinkedListConstIterator'将它的指针保存为'const LinkedListNode *',因此关于返回类型的决定更加明显:“const”变体遍历底层节点和值的常量视图 - 除非要允许编辑列表结构(例如,删除指向的元素通过一个常量迭代器),在这种情况下,您需要遍历节点的非常量视图,可能是'LinkedListNode *'。 –
seh
2014-10-09 20:05:00