使用enable_shared_from_this示例

时间:2025-04-17 13:06:39
 /*测试enable_shared_from_this*/
#include <iostream>
#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/smart_ptr/weak_ptr.hpp>
#include <boost/smart_ptr/enable_shared_from_this.hpp>
using namespace std; class TestClass : public enable_shared_from_this<TestClass>
{
public:
private:
int var;
}; void main()
{
TestClass *pobj = new TestClass(); shared_ptr<TestClass> sptr(pobj);
if (sptr==pobj->shared_from_this())
{
cout << "==" << endl;
}
else
{
cout << "!=" << endl;
} shared_ptr<TestClass> sptr2;
if (sptr2==pobj->shared_from_this())
{
cout << "==" << endl;
}
else
{
cout << "!=" << endl;
}
}