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被创建了一次,但是被释放了两次,相关文章推荐
- C++ - 函数模板(function template)右值引用参数 问题 及 解决
- C++ - 函数模板(function template)右值引用参数 问题 及 解决
- C++ 模板(template)
- using namespace
- C++”前置声明“那些事儿
- C++中的set和java的hashset有何区别?
- c/c++ 类中的成员变量声明时请优先考虑一下排列次序
- C++中创建持久对象的方法
- C++易错
- C++中对象的动态创建与释放
- C++中创建对象的两种方法及其区别
- C++多线程之使用Mutex和Critical_Section
- 基本语言细节--《The C++ Programming Language 》--(1)开辟鸿蒙
- c++知识点总结
- C++重载解析
- 栈和队列 C语言实现
- c/c++static
- C++之父谈C++--By BS
- 探寻C++最快的读取文件的方案【转…
- C++中继承方式和访问方式