您的位置:首页 > 编程语言 > C语言/C++

C++智能指针的非智能

2013-11-28 15:38 204 查看
#include <iostream>
#include <string>
#include "boost/lexical_cast.hpp"
#include <boost/shared_ptr.hpp>
using namespace std;
class A
{
public:
A()
{
cout<<"construct A"<<endl;
}
~A()
{
cout<<"delete A"<<endl;
}
};

void fun()
{
A * pA=new A();
A * &pA2=pA;
boost::shared_ptr<A> ptr(pA);
boost::shared_ptr<A>ptr2(pA2);
boost::shared_ptr<A> sp;

sp=ptr;
}
int main()
{
fun();
system("pause");
return 0;
}

1. 制造指针对引用无效,可能导致一个空间被释放多次,以上代码运行的结果

construct Adelete Adelete A对象A被创建了一次,但是被释放了两次,

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: