#include <string>
#include <iostream>
#include <boost/shared_ptr.hpp>
class implementation
{
public:
~implementation()
{
std::cout <<"destroying implementation ";
}
void do_something()
{
std::cout << "did something ";
}
};
void test()
{
boost::shared_ptr<implementation> sp1(new implementation());
std::cout<<"The Sample now has "<<sp1.use_count()<<" references ";
boost::shared_ptr<implementation> sp2 = sp1;
std::cout<<"The Sample now has "<<sp2.use_count()<<" references ";
sp1.reset();
std::cout<<"After Reset sp1. The Sample now has "<<sp2.use_count()<<" references ";
sp2.reset();
std::cout<<"After Reset sp2. ";
}
void main()
{
test();
}
该程序的输出结果如下:
The Sample now has 1 references
The Sample now has 2 references After Reset sp1.
The Sample now has 1 references destroying implementation After Reset sp2.
智能指针
最新推荐文章于 2024-04-21 00:21:54 发布