const_iteratorの実装の方法


ちょっと、自作イテレーター作るのに苦労した話をちょっと。
普通,iteratorならboost::iteratorsでも使ったら?なのだが、ちょっとそのiteratorは特殊だったので、自分で作ったほうがsmartに記述できるかな?と思い、自分で記述することにした。が、しかし、iteratorコンパイラに通っても、const_iteratorはとおらなかった。
何故だ。と思い、結構、四苦八苦していたのだが、分かった。

iteratorのコピーや比較のオーバーロードをすべて、constではないiteratorにすればよいかも!?*1
ゴメン、間違えてた。そうではないらしい。やっぱよくわからん。
ゴメン、さらに、間違えてた、もう、どっちがどっちだか。(´Д⊂グスン
よっしゃ!分かったわ!肝はfriendや!

と、言うことで擬似コード
Before


template
class iterator_base{
typedef iterator_base self_type;
iterator_base(const self_type &x){
copy処理
//return *this;
}
};
After

template
class iterator_base{
typedef iterator_base > iterator;
typedef iterator_base > const_iterator;
typedef iterator_base self_type;
//ここ!大切
friend iterator;
friend const_iterator;

iterator_base(const iterator &x){
copy処理
//return *this;
}
};

*1:多分。