boost::shared使用
程序员文章站
2022-03-03 19:13:19
...
#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;
}