plain-ptr.cpp
上传用户:qdkongtiao
上传日期:2022-06-29
资源大小:356k
文件大小:3k
源码类别:

书籍源码

开发平台:

Visual C++

  1. #include <iostream>
  2. using std::ostream; using std::cout; using std::endl;
  3. #include <string>
  4. // class that has a pointer member that behaves like a plain pointer
  5. class HasPtr {
  6. public:
  7.     friend ostream& operator<<(ostream&, const HasPtr&);
  8.     // copy of the values we're given
  9.     HasPtr(int *p, int i): ptr(p), val(i) { }
  10.     // const members to return the value of the indicated data member
  11.     int *get_ptr() const { return ptr; }     
  12.     int get_int() const { return val; }
  13.     // nonconst members to change the indicated data member
  14.     void set_ptr(int *p) { ptr = p; }
  15.     void set_int(int i) { val = i; }
  16.     // return or change the value pointed to, so ok for const objects
  17.     int get_ptr_val() const { return *ptr; } 
  18.     void set_ptr_val(int val) const { *ptr = val; }
  19. private:
  20.     int *ptr;
  21.     int val;
  22. };
  23. void f3()
  24. {
  25. int i = 42;
  26. HasPtr p1(&i, 42);
  27. HasPtr p2 = p1;
  28. cout << p2.get_ptr_val() << endl;
  29. p1.set_ptr_val(0);
  30. cout << p2.get_ptr_val() << endl;
  31. }
  32. int main()
  33. {
  34. int obj = 0;
  35. HasPtr ptr1(&obj, 42);  // int* member points to obj, val is 42
  36. HasPtr ptr2(ptr1);      // int* member points to obj, val is 42
  37. cout << "(1) ptr1: " << ptr1 << endl << "ptr2: " << ptr2 << endl;
  38. ptr1.set_ptr_val(42); // sets object to which both ptr1 and ptr2 point
  39. ptr2.get_ptr_val();   // returns 42
  40. cout << "(2) ptr1: " << ptr1 << endl << "ptr2: " << ptr2 << endl;
  41. ptr1.set_int(0);   // changes val member only in ptr1
  42. ptr2.get_int();    // returns 42
  43. ptr1.get_int();    // returns 0
  44. cout << "(3) ptr1: " << ptr1 << endl << "ptr2: " << ptr2 << endl;
  45. int *ip = new int(42); // dynamically allocated int initialized to 42
  46. HasPtr ptr(ip, 10);    // HasPtr points to same object as ip does
  47. delete ip;             // object pointed to by ip is freed
  48. ptr.set_ptr_val(0); // disaster: The object to which HasPtr points was freed!
  49. cout << "(4) ptr: " << ptr << endl;
  50. f3();
  51. return 0;
  52. }
  53. void f(int *p) 
  54. { // new socpe
  55.     // allocates new int to hold a copy of the object to which p points
  56.     HasPtr local_copy(p, 0);
  57.     // . . .
  58. } // local_copy goes out of scope
  59. void f2()
  60. {
  61.     int obj= 42;
  62.     HasPtr local1(&obj, 0); // allocates a new int to hold a copy of obj
  63.     if (obj) { // new scope
  64.         HasPtr local2(local1); // local1 and local2 hold same pointer
  65.         // . . .
  66.     } // local2 goes out of scope, object to which it points is freed
  67.     local1.set_ptr_val(0);  // disaster -- the object to which local1 points was freed!
  68. }
  69. ostream& operator<<(ostream &os, const HasPtr &hp)
  70. {
  71.     cout << "*ptr: " << hp.get_ptr_val() << "tval: " << hp.get_int() << endl;
  72.     return os;
  73. }