源自《C++ primer》第四版 p419
- // class that has a pointer member that behaves like a plain pointer
- class HasPtr {
- public:
- // copy of the values we're given
- HasPtr(int *p, int i): ptr(p), val(i) { }
- // const members to return the value of the indicated data member
- int *get_ptr() const { return ptr; }
- int get_int() const { return val; }
- // non const members to change the indicated data member
- void set_ptr(int *p) { ptr = p; }
- void set_int(int i) { val = i; }
- // return or change the value pointed to, so ok for const objects
- int get_ptr_val() const { return *ptr; }
- void set_ptr_val(int val) const { *ptr = val; }
- private:
- int *ptr;
- int val;
- };
- int obj = 0;
- HasPtr ptr1(&obj, 42); // int* member points to obj, val is 42
- HasPtr ptr2(ptr1); // int* member points to obj, val is 42
- int obj;
- HasPtr p1(&obj, 42);
- HasPtr p2(p1); // p1 and p2 both point to same int object
- HasPtr p3(p1); // p1, p2, and p3 all point to same int object
- class U_Ptr {
- friend class HasPtr;
- int *ip;
- size_t use;
- U_Ptr(int *p): ip(p), use(1) { }
- ~U_Ptr() { delete ip; }
- };
- //private class for use by HasPtr only
- class U_Ptr {
- friend class HasPtr;
- int *ip;
- size_t use;
- U_Ptr(int *p): ip(p), use(1) { }
- ~U_Ptr() { delete ip; }
- };
- class HasPtr {
- public:
- // HasPtr owns the pointer; p must have been dynamically allocated
- HasPtr(int *p, int i): ptr(new U_Ptr(p)), val(i) { }
- // copy members and increment the use count
- HasPtr(const HasPtr &orig):
- ptr(orig.ptr), val(orig.val) { ++ptr->use; }
- HasPtr& operator=(const HasPtr&);
- // if use count goes to zero, delete the U_Ptr object
- ~HasPtr() { if (--ptr->use == 0) delete ptr; }
- friend ostream& operator<<(ostream&, const HasPtr&);
- // copy control and constructors as before
- // accessors must change to fetch value from U_Ptr object
- int *get_ptr() const { return ptr->ip; }
- int get_int() const { return val; }
- // change the appropriate data member
- void set_ptr(int *p) { ptr->ip = p; }
- void set_int(int i) { val = i; }
- // return or change the value pointed to, so ok for const objects
- // Note: *ptr->ip is equivalent to *(ptr->ip)
- int get_ptr_val() const { return *ptr->ip; }
- void set_ptr_val(int i) { *ptr->ip = i; }
- private:
- U_Ptr *ptr; // points to use-counted U_Ptr class
- int val;
- };
- HasPtr& HasPtr::operator=(const HasPtr &rhs)
- {
- ++rhs.ptr->use; // increment use count on rhs first
- if (--ptr->use == 0)
- delete ptr; // if use count goes to 0 on this object, delete it
- ptr = rhs.ptr; // copy the U_Ptr object
- val = rhs.val; // copy the int member
- return *this;
- }
- class HasPtr {
- public:
- // no point to passing a pointer if we're going to copy it anyway
- // store pointer to a copy of the object we're given
- HasPtr(const int &p, int i): ptr(new int(p)), val(i) {}
- // copy members and increment the use count
- HasPtr(const HasPtr &orig):
- ptr(new int (*orig.ptr)), val(orig.val) { }
- HasPtr& operator=(const HasPtr&);
- // wrong: don't define a destructor without also defining copy and assign
- ~HasPtr() { delete ptr; }
- friend ostream& operator<<(ostream&, const HasPtr&);
- // accessors must change to fetch value from Ptr object
- int get_ptr_val() const { return *ptr; }
- int get_int() const { return val; }
- // change the appropriate data member
- void set_ptr(int *p) { ptr = p; }
- void set_int(int i) { val = i; }
- // return or change the value pointed to, so ok for const objects
- int *get_ptr() const { return ptr; }
- void set_ptr_val(int p) const { *ptr = p; }
- private:
- int *ptr; // points to an int
- int val;
- };
- HasPtr& HasPtr::operator=(const HasPtr &rhs)
- {
- // Note: Every HasPtr is guaranteed to point at an actual int;
- // We know that ptr cannot be a zero pointer
- *ptr = *rhs.ptr; // copy the value pointed to
- val = rhs.val; // copy the int
- return *this;
- }