BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / cpp / #20285同步于 2009/3/16
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖

管理具有指针成员的类--智能指针类

tianma25
2009/3/16镜像同步2 回复
/* smart pointer class: takes ownership of the dynamically allocated * object to which it is bound * User code must dynamically allocate an object to initialize a HasPtr * and must not delete that object; the HasPtr class will delete it */ //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; } 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; } int main() { int obj = 0; HasPtr ptr1(&obj, 42); HasPtr ptr2(ptr1); //... } 以上是经过删减的C++ primer中的例子,我在Visual Studio里运行时会出现“Debug assertion failed”的错误,看了半天发现了最上面一句话 User code must dynamically allocate an object to initialize a HasPtr and must not delete that object; the HasPtr class will delete it 然后我把main()中的定义改为了 HasPtr *ptr1=new HasPtr(&obj, 42); HasPtr *ptr2=ptr1; 再运行就没错误了,但还是不明白两个地方,一是为什么要像这样定义才行; 二是像这样定义后,重载的赋值操作符没有被调用到,那重载这个操作符有什么意义。
订阅后,新回复会通过你的通知中心匿名送达。
2 条回复
ttplayer机器人#1 · 2009/3/16
【 在 tianma25 的大作中提到: 】 : /* smart pointer class: takes ownership of the dynamically allocated : * object to which it is bound : * User code must dynamically allocate an object to initialize a HasPtr : * and must not delete that object; the HasPtr class will delete it : */ : //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; } : 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; : } : int main() : { : int obj = 0; : HasPtr ptr1(&obj, 42); : HasPtr ptr2(ptr1); : //... : } : 以上是经过删减的C++ primer中的例子,我在Visual Studio里运行时会出现“Debug assertion failed”的错误,看了半天发现了最上面一句话 : User code must dynamically allocate an object to initialize a HasPtr and must not delete that object; the HasPtr class will delete it : 然后我把main()中的定义改为了 : HasPtr *ptr1=new HasPtr(&obj, 42); : HasPtr *ptr2=ptr1; : 再运行就没错误了,但还是不明白两个地方,一是为什么要像这样定义才行; : 二是像这样定义后,重载的赋值操作符没有被调用到,那重载这个操作符有什么意义。 : 大哥,不是这么玩的。 你的这个HasPtr是用来管new出来的东西的, 一个堆栈上的int obj又不要你手动修理吧? int int_ptr = new int(100); HasPtr has_pat(int_ptr); //这里就不用delete int_ptr了。 //hasptr给你解决了
zieckey机器人#2 · 2009/3/16
delete只对堆上的数据操作, 对栈上的数据操作会出错的。。。 【 在 ttplayer 的大作中提到: 】 : : : : : ...................