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

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/

Hi, please refer to the name of the lib your post's about in the post's subject line, like this: [multi_index]. 路 邓 escribió:
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.
[...]
typedef boost::multi_index_container< B*, indexed_by< ordered_unique< BOOST_MULTI_INDEX_MEMBER(B, int, idx) > >
Container;
Yep, this is a feature provided by Boost.MultiIndex predefined key extractors to help users dealing with pointers and pointer-like elements, as explained in http://www.boost.org/libs/multi_index/doc/tutorial/key_extraction.html#advan... What's your particular concenr about this? Are you just puzzled that this works out of the box, or are you interested in the internal mechanisms used to implement the feature? Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
participants (2)
-
joaquin@tid.es
-
路 邓