ポインタのポインタが返ってくるでござる
STLのiteratorにおけるoperator->()とoperator*()に関して。
例えば、
Handleクラスのポインタを格納したコンテナsetがある場合、
イテレータに対する*itは参照を返してくれるので「Handle*」が得られるが、
イテレータに対するit->はポインタを返すので、「Handle* const*」が得られる。
(ただのポインタが格納されている時はポインタ自身がconstであり、constなオブジェクトを指すポインタではないので注意)
故に、(*it)->setTitle ...などというように参照からHandle*を得ないと操作できない。(ことはないけどな!
#include <iostream> #include <set> using namespace std; class Handle { public: Handle(int id) : id_(id) {} int id() const { return id_; } private: int id_; }; struct HandleLess : public binary_function<Handle*, Handle*, bool> { bool operator()(const Handle* lhs, const Handle* rhs) const { return lhs->id() < rhs->id(); } }; typedef set<Handle*, HandleLess> HandleSet; int main() { HandleSet handleSet; for (int i = 0; i < 100; ++i) { handleSet.insert(new Handle(i)); } for (HandleSet::iterator it = handleSet.begin(); it != handleSet.end(); ++it) { //cout << (*it)->id() << endl; Handle* const* handle = it.operator->(); cout << (*handle)->id() << endl; } // メモリなんて解放してやるもんか! return 0; }