Boost logo

Boost :

Subject: [boost] Feed pointers to a container, however use the member of the data type which pointer refer to as an index
From: è·¯ é‚“ (denglu36_at_[hidden])
Date: 2009-09-21 22:33:52

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
    int idx;

typedef boost::multi_index_container<
            BOOST_MULTI_INDEX_MEMBER(B, int, idx)

int main()
    Container container;
    B* p1 = new B;
    B* p2 = new B;
    p1->idx = 2;
    p2->idx = 1;
    Container::iterator iter;
        B* tmp = (*iter);
        std::cout << tmp->idx << std::endl;
    return 0;

Boost list run by bdawes at, gregod at, cpdaniel at, john at