经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C » 查看文章
c/c++ 标准库 智能指针是啥玩意儿
来源:cnblogs  作者:小石王  时间:2018/9/26 18:01:11  对本文有异议

标准库 智能指针( smart pointer ) 是啥玩意儿

一,为什么有智能指针???

c++程序员需要自己善后自己动态开辟的内存,一旦忘了释放,内存就泄露。

智能指针可以帮助程序员"自动释放"自己开辟的内存。

二,从哪里看出来智能了???

  1. int *= new int(11);
  2. auto_ptr<int> pa(p);//auto_ptr已经不推荐使用
  3. //delete p;

上面的代码把p交给智能指针auto_ptr管理后,就不需要自己去delete p。auto_ptr会去释放p,所以体现出了"智能"

三,哪里看起来像指针了???

  1. int *= new int(11);
  2. my_auto_ptr<int> pa(p);                                                                
  3. *pa = 111;
  4. cout << *pa << endl;
  5. cout << *<< endl;

上面的代码对智能指针pa使用了,*运算符,并且通过pa改变了p的值,所以看起来像指针哦。

  1. class Test{
  2. public:
  3.   void fun(){
  4.     cout << "func()" << endl;
  5.   }
  6. };
  7.  
  8. Test* pt = new Test;
  9. auto_ptr<Test> pa1(pt);
  10. pa1->fun();

上面的代码对智能指针pa1使用了,->运算符,并且通过pa1调用了对象pt的fun()成员方法,所以看起来像指针哦。

四,智能指针到底是个啥玩意儿???

是个模板类。

五,智能指针的是怎么实现智能指针的?

  • 在模板类里重写operator* 运算符

  • 在模板类里重写operator->运算符

  • 管理指针的所有权和转移(下面的例子没有实现)

  1. #include <iostream>
  2. #include <memory>
  3.  
  4. using namespace std;
  5.  
  6. template<typename T>
  7. class my_auto_ptr{
  8. public:
  9.   my_auto_ptr(T* p = nullptr):own(p!=nullptr),ptr(p){}
  10.   ~my_auto_ptr(){
  11.     if(own){
  12.       delete ptr;
  13.     }
  14.   }
  15.   T& operator*()const{
  16.     return *ptr;
  17.   }
  18.   T* operator->()const{
  19.     return ptr;
  20.   }
  21. private:
  22.   bool own;
  23.   T* ptr;
  24. };
  25. class Test{
  26. public:
  27.   void fun(){
  28.     cout << "func()" << endl;
  29.   }
  30. };
  31. int main(){
  32.   //test1 老版本的auto_ptr的使用,现在不推荐使用                                
  33.   /*                                                                            
  34.   int *p = new int(10);                                                         
  35.   auto_ptr<int> pa(p);                                                          
  36.   cout << *pa << endl;                                                          
  37.                                                                                 
  38.   string* ps = new string("aaa");                                               
  39.   auto_ptr<string> pas(ps);                                                     
  40.   cout << pas->size() << endl;                                                  
  41.   */
  42.  
  43.   //test2 自己实现auto_ptr                                                      
  44.   int *= new int(11);
  45.   my_auto_ptr<int> pa(p);
  46.   //delete p;                                                                   
  47.   *pa = 111;
  48.   cout << *pa << endl;
  49.   cout << *<< endl;
  50.  
  51.   Test* pt = new Test;
  52.   my_auto_ptr<Test> pa1(pt);
  53.   pa1->fun();
  54.  
  55.   return 0;
  56. }

github完整代码

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号