私は独自のジェネリック ツリーの実装を作成しましたが、イテレータを作成するときに const の正確性に問題があります。私が現在抱えている問題は次のとおりです。
これは、私が書いた DFS イテレータのヘッダー ファイルです。
template<class Item>
class DFSIterator
{
public:
DFSIterator(const Item& rRootNode);
~DFSIterator();
DFSIterator* First();
DFSIterator* operator++(int rhs);
Item* operator*() const;
Item* operator->() const;
bool isDone() const;
template <class Node> friend class Node;
private:
void initListIterator(const Item* currentNode);
bool m_bIsDone;
const Item* m_pRootNode;
const Item* m_pCurrentNode;
ListIterator<Item>* m_pCurrentListIter;
std::map<const Item*, ListIterator<Item>*> m_listMap;
};
したがって、私が懸念しているのは、間接参照演算子です。
template<class Item>
Item* DFSIterator<Item>::operator*() const
{
if(isDone())
{
return NULL;
}
else
{
return const_cast<Item*>(m_pCurrentNode);
}
}
そこで const_cast を実行するのは適切ですか? ユーザーが const オブジェクトをコンテナに入れると、これが問題を引き起こすかどうか疑問に思っていますか?