(ZT) Is it legal (and moral) for a member function to say delete this?
发布日期:2021-06-30 22:07:55
浏览次数:3
分类:技术文章
本文共 1274 字,大约阅读时间需要 4 分钟。
original url from : https://isocpp.org/wiki/faq/freestore-mgmt#delete-this
As long as you’re careful, it’s okay (not evil) for an object to commit suicide (delete
this
).
Here’s how I define “careful”:
- You must be absolutely 100% positively sure that
this
object was allocated vianew
(not bynew[]
, nor by , nor a local object on the stack, nor a namespace-scope / global, nor a member of another object; but by plain ordinarynew
). - You must be absolutely 100% positively sure that your member function will be the last member function invoked on
this
object. - You must be absolutely 100% positively sure that the rest of your member function (after the
delete
this
line) doesn’t touch any piece ofthis
object (including calling any other member functions or touching any data members). This includes code that will run in destructors for any objects allocated on the stack that are still alive. - You must be absolutely 100% positively sure that no one even touches the
this
pointer itself after thedelete
this
line. In other words, you must not examine it, compare it with another pointer, compare it withnullptr
, print it, cast it, do anything with it.
Naturally the usual caveats apply in cases where your this
pointer is a pointer to a base class when you don’t have a .
转载地址:https://lostspeed.blog.csdn.net/article/details/50346115 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
网站不错 人气很旺了 加油
[***.192.178.218]2024年04月26日 11时51分16秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
shell dirname basename
2019-05-01
eureka rest operations
2019-05-01
线程分析
2019-05-01
jenkins
2019-05-01
DOM(总结)
2019-05-01
JS判断一个数据的数据类型
2019-05-01
MySQL学习总结(一)
2019-05-01
MySQL学习总结(二)
2019-05-01
MySQL学习总结(三)
2019-05-01
未来已至,5G加持下的云游戏将走向何方?
2019-05-01
Mysql常用删除方式比较
2019-05-01
备案问题汇总
2019-05-01
“头号玩家”距离我们究竟有多远?
2019-05-01
首批!金山云获得可信金融云解决方案认证
2019-05-01
金山云张斯聪:探索沉浸式解决方案 提升教育云上体验
2019-05-01
高防弹性IP 金山云荣获“可信云技术最佳实践--网络安全”奖
2019-05-01
一周拿下百度Offer!211本+985硕+计算机专业~
2019-05-01
闭关三月!猛男逆道而行,四杀斩获阿里 / 腾讯 / 京东 / 百度等大厂 offer
2019-05-01
上线三天获 22w 浏览量!2021 最新一线大厂 Java 高级架构师面试题总结~
2019-05-01