理解和使用C++中的智能指针
来源:互联网 发布:药学知乎 编辑:程序博客网 时间:2024/05/21 04:00
使用C++一个最大的好处是它的动态性。我们可以使用new ,delete 动态分配内存和释放内存。但是有时候就会出现一些问题,如果我们忘记删除用new创建的对象,将会出现内存泄露,在长期运行的系统中,甚至一个很小的内存泄露将会造成很大的问题。所以,怎么样保证每一个new对应一个delete呢?
一个很好的办法是用C++中的auto_ptr变量。接下来讲使用智能指针来解决这个问题。 在MyAuto_Ptr类中实现智能指针,用类
MyTestObect 测试智能指针。
测试类:MyTestObect
class MyTestObject{ string name; int age;public: MyTestObject(string s, int a) { name = s; age = a; cout << "Object created\n"; } ~MyTestObject(void) { cout << "Object destroyed\n"; } string GetName() { return name; } int GetAge() { return age; }};智能指针类:MyAuto_Ptr
获取指针
template <class T>class MyAuto_Ptr{private: T *ptr;public: //构造函数 explicit MyAuto_Ptr(T *p = 0) :ptr(p) { } //析构函数 ~MyAuto_Ptr(void) { delete ptr; } //复制构造函数 MyAuto_Ptr(MyAuto_Ptr<T> &source) { ptr = source.ptr; //Since the ownership gets transferred lets point the source to 0 source.ptr = 0; } MyAuto_Ptr& operator=(MyAuto_Ptr<T> &source) { //check for self reference if(ptr != source.ptr) { //flush the existing memory delete ptr; ptr = 0; // use the new pointer ptr = source.ptr; //Since the ownership gets transferred lets point the source to 0 source.ptr = 0; return *this; } } //获取指针 T& operator *() { return *ptr; } // T* operator ->() { return ptr; } //获取指针的值 T* get() { return ptr; } //重设指针的值 void reset(T *newVal) { delete ptr; ptr = newVal; } //release the pointer and returns the ownership T* release() { T *t = ptr; ptr = 0; return t; }};
测试代码:
int main(int argc, char* argv[]){ {//dummy brace to test the destruction MyAuto_Ptr<MyTestObject> p(new MyTestObject("Rahul Singh", 29)); //test the -> operator cout << "Name: " << p->GetName() << ", Age: " << p->GetAge() << "\n"; //test the transfer of ownership in copy constructor MyAuto_Ptr<MyTestObject> p2(p); //test the * operator cout << "Name: " << (*p2).GetName() << ", Age: " << (*p2).GetAge() << "\n"; //test the transfer of ownership in assignment operator MyAuto_Ptr<MyTestObject> p3; p3 = p2; //test the get function cout << "Name: " << p3.get()->GetName() << ", Age: " << p3.get()->GetAge() << "\n"; //lets test the reset function p3.reset(new MyTestObject("Megha Singh", 28)); //test the get function cout << "Name: " << p3.get()->GetName() << ", Age: " << p3.get()->GetAge() << "\n"; //finally lets test the release function MyAuto_Ptr<MyTestObject> p4(p3.release()); cout << "Name: " << p4.get()->GetName() << ", Age: " << p4.get()->GetAge() << "\n"; } getchar(); return 0;}
复制搜索
复制搜索
- 理解和使用C++中的智能指针
- vtk智能指针->对智能指针的理解和使用
- C ++中的智能指针
- C/C++: stl 和 boost 中的智能指针
- C++:智能指针-TR1的shared_ptr和weak_ptr使用介绍
- C++:智能指针-TR1的shared_ptr和weak_ptr使用介绍
- 深入理解C++中的智能指针auto_ptr
- 彻底搞清c/c++中的几个指针概念:悬垂指针和智能指针以及哑指针和野指针
- 彻底搞清c/c++中的几个指针概念:悬垂指针和智能指针以及哑指针和野指针
- 【C/C++和指针】auto_ptr智能指针
- 智能指针的理解 和 简单实现
- c中的指针的理解
- C语言中的指针理解
- 怎样管理C++类中的指针成员 和 简单的c++智能指针使用的例子
- android中的智能指针:强指针和弱指针
- Android中的智能指针:强指针和弱指针
- 【C++】Vs2008中使用智能指针
- Android中的智能指针:sp和wp
- DBLink创建方法
- web messaging
- Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)
- 常用的android intent和intent action
- c#asp.net 读取 写入 txt 文件 asp.net操作文件
- 理解和使用C++中的智能指针
- 想要清晨不赖床?11个超实用妙招!
- vs2005 timer控件的interval值最大到底是多少?
- 友情链接交换三要素:让网站细水长流
- java程序员从笨鸟到菜鸟之(七)一—java数据库操作
- erlang-gen_tcp手册(翻译)
- CSS中cellpadding和cellspacing的代替方法
- 学习android JNI的那些事儿--------6. 在java中传入引用数据类型(对象)作为参数
- eclipse更改workspace路径