1.永恒的话题

内存泄漏(臭名昭著的Bug)
—-动态申请堆空间,用完后不归还
—-c++语言中没有垃圾回收机制
—-指针无法控制所指堆空间的生命周期

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. class Test
  5. {
  6. int i;
  7. public:
  8. Test(int i)
  9. {
  10. this->i = i;
  11. }
  12. int value()
  13. {
  14. return i;
  15. }
  16. ~Test()
  17. {
  18. }
  19. };
  20. int main()
  21. {
  22. for(int i=0; i<5; i++)
  23. {
  24. Test* p = new Test(i); //用完未归还,就会导致内存泄漏
  25. cout << p->value() << endl;
  26. }
  27. return 0;
  28. }

2.深度的思考

我们需要什么
—-需要一个特殊的指针
—-指针生命周期结束时主动释放堆空间
—-一片堆空间只能由一个指针标识(避免多次释放的问题)
—-杜绝指针运算和指针比较

3.解决方案

—-重载指针特征操作符(->和*)
—-只能通过类的成员函数重载
—-重载函数不能使用参数(只能定义一个重载函数)
—-只能定义一个重载函数

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. class Test
  5. {
  6. int i;
  7. public:
  8. Test(int i)
  9. {
  10. cout << "Test(int i)" << endl;
  11. this->i = i;
  12. }
  13. int value()
  14. {
  15. return i;
  16. }
  17. ~Test()
  18. {
  19. cout << "~Test()" << endl;
  20. }
  21. };
  22. class Pointer
  23. {
  24. Test* mp;
  25. public:
  26. Pointer(Test* p = NULL) //被堆空间上的类初始化
  27. {
  28. mp = p;
  29. }
  30. Test* operator ->()
  31. {
  32. return mp;
  33. }
  34. Test& operator * ()
  35. {
  36. return *mp;
  37. }
  38. ~Pointer()
  39. {
  40. delete mp;
  41. }
  42. };
  43. int main()
  44. {
  45. for(int i=0; i<5; i++)
  46. {
  47. Pointer p = new Test(i); //用一个类对象来模拟指针
  48. cout << p->value() << endl;
  49. }
  50. return 0;
  51. }
  52. result:
  53. delphi@delphi-vm:~$ ./a.out
  54. Test(int i)
  55. 0
  56. ~Test()
  57. Test(int i)
  58. 1
  59. ~Test()
  60. Test(int i)
  61. 2
  62. ~Test()
  63. Test(int i)
  64. 3
  65. ~Test()
  66. Test(int i)
  67. 4
  68. ~Test()
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. class Test
  5. {
  6. int i;
  7. public:
  8. Test(int i)
  9. {
  10. cout << "Test(int i)" << endl;
  11. this->i = i;
  12. }
  13. int value()
  14. {
  15. return i;
  16. }
  17. ~Test()
  18. {
  19. cout << "~Test()" << endl;
  20. }
  21. };
  22. class Pointer
  23. {
  24. Test* mp;
  25. public:
  26. Pointer(Test* p = NULL) //被堆空间上的类初始化
  27. {
  28. mp = p;
  29. }
  30. Pointer(const Pointer& obj)
  31. {
  32. //delete mp;
  33. mp = obj.mp;
  34. const_cast<Pointer&>(obj).mp = NULL; //所有权的转移,初始化对象将所有权交接给当前对象
  35. }
  36. Pointer& operator = (const Pointer& obj)
  37. {
  38. if(this != &obj)
  39. {
  40. //delete mp;
  41. mp = obj.mp;
  42. const_cast<Pointer&>(obj).mp = NULL;
  43. }
  44. return *this;
  45. }
  46. Test* operator ->()
  47. {
  48. return mp;
  49. }
  50. Test& operator * ()
  51. {
  52. return *mp;
  53. }
  54. bool isNull()
  55. {
  56. return (mp == NULL);
  57. }
  58. ~Pointer()
  59. {
  60. delete mp;
  61. }
  62. };
  63. int main()
  64. {
  65. Pointer p1 = new Test(0);
  66. cout << p1->value() << endl;
  67. Pointer p2 = p1; //p1将堆空间转给了p2
  68. cout << p1.isNull() << endl;
  69. cout << p2->value() << endl;
  70. return 0;
  71. }

4.智能指针的使用军规

智能用来指向堆空间中的对象或者变量,不能指向栈空间
最大的特点:在指针结束时释放对应的堆空间
智能指针使得可以用对象来代替指针

小结:
1.指针特征操作符(->和*)可以被重载
2.重载指针特征操作符能够使用对象代替指针
3.智能指针智能指向堆空间的内存
4.智能指针的意义在于最大程度的避免内存问题