Feed pointers to a container, however use the member of the data type which pointer refer to as an index

Dear All: In the following codes, I feed the container with B*, however I user the member of B as the index. Can somebody explain to me how this work out? And It seems that if smart pointers are feeded into the container, it still works out. #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */ #include <boost/multi_index_container.hpp> #include <boost/multi_index/ordered_index.hpp> #include <boost/multi_index/member.hpp> #include <iostream> using namespace boost::multi_index; class B { public: int idx; }; typedef boost::multi_index_container< B*, indexed_by< ordered_unique< BOOST_MULTI_INDEX_MEMBER(B, int, idx) > >
Container;
int main() { Container container; B* p1 = new B; B* p2 = new B; p1->idx = 2; p2->idx = 1; container.insert(p1); container.insert(p2); Container::iterator iter; for(iter=container.begin();iter!=container.end();iter++) { B* tmp = (*iter); std::cout << tmp->idx << std::endl; } return 0; } 好玩贺卡等你发,邮箱贺卡全新上线! ___________________________________________________________ 好玩贺卡等你发,邮箱贺卡全新上线! http://card.mail.cn.yahoo.com/
participants (1)
-
路 邓