我的常量和非常量链表迭代程序有何不同



我有四个类,其中两个是迭代器:

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_iterators是如何为任何STL容器工作的(例如:std::list)。如果您正在寻找类似的迭代器类,那么是的,您应该返回一个对常量值的引用,即const T&

相关内容

最新更新