예제 #1
0
void weakptr_study() {
	{
		std::shared_ptr<int> kid2(new int(5));
	}
	WDummy dd;
	std::shared_ptr<WDummy> spr2 = dd.process();

	std::shared_ptr<int> kid(new int(10));
	
	
	std::weak_ptr<int> ptr1 = kid;
	//std::cout << ptr1.use_count() << std::endl;
	std::weak_ptr<int> ptr2;
	{
		std::shared_ptr<int> kid2(new int(10));
		ptr2 = kid2;
	}
	if (ptr1.expired()) {
		std::cout << "ptr1 dead" << std::endl;
	}
	if (ptr2.expired()) {
		std::cout << "ptr2 dead" << std::endl;
	}

	// convert	
	std::shared_ptr<int> sptr2(ptr2);
	//std::shared_ptr<int> sptr4 = ptr2;
	auto sptr1 = ptr2.lock();
	std::shared_ptr<int> sptr3 = ptr2.lock();

}
예제 #2
0
int main()
{
    std::shared_ptr<int> sptr1 = std::make_shared<int>(100);
    std::shared_ptr<int> sptr2 (new int);

    return 0;
}
예제 #3
0
//Unlike auto_ptr, scoped_ptr inhibits copy/assignment, with compile errors.
//auto_ptr will delete the old copy once a new value is assigned.
int main(int argc, char ** argv){
	std::cout<<"Scoped ptr example starting"<<std::endl;
	boost::scoped_ptr<Base> bptr1(new Base("obj1"));
	{
		boost::scoped_ptr<Base> bptr2(new Base("obj2"));
		boost::scoped_ptr<Base> bptr3(new Base("obj3"));
		//bptr2=bptr3; //will not compile
	}
	std::auto_ptr<Base> sptr1(new Base("obj4"));
	std::auto_ptr<Base> sptr2(new Base("obj5"));
	sptr1=sptr2; //auto_ptr will delete the object names obj4.
	sptr1=std::auto_ptr<Base>(new Base("obj6"));//object names obj5 will be deleted.
	std::cout<<"Scoped ptr example exiting"<<std::endl;
	return 0;
}