-
//[C++ Boost] boost::enable_shared_from_this ex1
//
// 6125 jyj
//
#include <iostream>
//#include <memory>
#include "boost/enable_shared_from_this.hpp"
#include <boost/shared_ptr.hpp>
#include <cassert>
//자신의 클래스 내부에서 자신의 shared_ptr<T> 값을 생성해 반환 해 주는 단순한 도구.
//단 자신은 shared_ptr로 생성되었어야 한다.
namespace BoostEnable_shared_from_thisTest01
{
//from http://www.boost.org/doc/libs/1_55_0/libs/smart_ptr/enable_shared_from_this.html
class Y: public boost::enable_shared_from_this<Y>
{
public:
int v1;
~Y()
{
std::cout << "class destroy " << std::endl;
}
boost::shared_ptr<Y> sp()
{
return shared_from_this();
}
};
void Main()
{
std::cout << __FILE__ << std::endl;
{
boost::shared_ptr<Y> q1;
{
boost::shared_ptr<Y> q2;
{
boost::shared_ptr<Y> p(new Y);
p->v1 = 3;
q1 = p->sp();
q2 = p;
assert(p == q1);
assert(!(p < q1 || q1 < p)); // p and q must share ownership
}
std::cout << "val " << q1->v1 << std::endl;
assert(q1 == q2);
}
std::cout << "val " << q1->v1 << std::endl;
q1->v1 = 1;
}
std::cout << "end " << std::endl;
}
}