我爱Aspx >> VC.Net >> Don Box:Smart Pointers : What , Why , Which?
Don Box:Smart Pointers : What , Why , Which?
- 作者:aspxer 来源:internet 日期:2007-5-21 23:48:24 关键字:
- * p(new MyClass
); MyClass
* q = p; delete p; p->DoSomething
(); // Watch out! p is now dangling! p = NULL;
// p is no longer dangling q->DoSomething
(); // Ouch! q is still dangling!For auto_ptr, this is solved by setting its pointer to NULL when it is copied:
template <class T> auto_ptr
<T>& auto_ptr
<T>::operator=
(auto_ptr
<T>& rhs) { if (this != &rhs) { delete ptr; ptr = rhs.ptr; rhs.ptr = NULL; } return *this; }
Other smart pointers may do other things when they are copied. Here are some possible strategies for handling the statement q = p, where p and q are smart pointers: - Create a new copy
of the object pointed by p, and have q point to this copy. This strategy is implemented in .
- Ownership transfer
【我对这篇文章有话说?】
- 广告位招租,广告代号:content_468_15
- 上一篇:让应用程序从IE中获取键入的网址
下一篇:企业上网的几个理由
- 关于我们 | 广告服务 | 网站建设 | 版权申明 | 申请链接 | 联系我们
- Copyright © 2007 5iaspx.COM. All Right Reserved.
- 本站内容仅供学习,研究,探讨,个人收藏,如有侵权,敬请联系我们,我们尽快解决!