کلاس enable_shared_from_this یک تابع public داره به اسم shared_from_this که کارش این هست که یک shared pointer به همین کلاس که داخلش هستیم بر می گردونه پس وقتی که از این کلاس ارث می بریم این تابع هم قابل استفاده میشه .
#include <boost/enable_shared_from_this.hpp>
#include <boost/shared_ptr.hpp>
class Test : public boost::enable_shared_from_this<Test>
{
public:
boost::shared_ptr<Test> get_instance()
{
return shared_from_this();//in function az enable_shared_from_this hast
}
};
int main()
{
boost::shared_ptr<Test> t(new Test);
boost::shared_ptr<Test> t2 = t->get_instance();
}
ضمنا این کلاس به c++11 هم اضافه شده:
http://en.cppreference.com/w/cpp/memory/enable_shared_from_this