shared_ptr + bind + map + for_each 코드


#include <iostream>
#include <map>
#include <algorithm>
#include <boost/shared_ptr.hpp>
#include <boost/bind.hpp>

class A
{
public:
    A(int num) : m_num(num) { std::cout << "A created.." << std::endl; }
    ~A() { std::cout << "A destroyed.." << std::endl; }
    void print() { std::cout << m_num << " = scream!!" << std::endl; }
protected:
    int m_num;
};

class B : public A
{
public:    
    B(int num) : A(num) { std::cout << m_num << " = B created.." << std::endl; }
    ~B() { std::cout << m_num << "= B destroyed.." << std::endl; }
};

int main()
{
    std::map<const char*, boost::shared_ptr<A> > testmap;
    testmap["01"] = boost::shared_ptr<A>(new B(1));
    testmap["02"] = boost::shared_ptr<A>(new B(2));
    testmap["03"] = boost::shared_ptr<A>(new B(3));
    testmap["04"] = boost::shared_ptr<A>(new B(4));
    testmap["05"] = boost::shared_ptr<A>(new B(5));

    std::for_each(testmap.begin(), testmap.end(),
        boost::bind(&A::print,
        boost::bind(&std::map<const char*, boost::shared_ptr<A> >::value_type::second, _1)));
    return 0;
}

제대로 동작할거라는 확신이 필요해서 아침에 테스트를 간단히 해봤다.

트랙백

이 글과 관련된 글 쓰기 (트랙백 보내기)
TrackbackURL : http://xeraph.com/tb/1858819 [도움말]

덧글

댓글 입력 영역