boost::shared使用

 

#include <stdio.h>
#include <boost/shared_ptr.hpp>
#include <string>
#include <iostream>
using namespace std;

class A {
public:
	A(string a)
	{
		cout << "Hello " << a << endl;
	}
	void print();
};

void A::print()
{
	printf("this address: 0x%x\n", this);
}

int main() 
{
	//栈空间,系统自动释放
	A a0(string("a0"));
	a0.print();

	//堆空间,shared_ptr释放
	boost::shared_ptr<A> a1(new A(string("a1")));
	a1->print();

	//堆空间,手动释放
	A* a2 = new A(string("a2"));
	a2->print();
	delete(a2);

	return 0;
}

 


版权声明:本文为zlf19910726原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。