弱/强引用指针关系
我一直在尝试编写自己的弱/强指针,但我并不清楚其中的关系。我遇到的一切似乎都没有说清楚,而且经常有一位医生会反驳另一位医生所说的。谁能详细解释一下弱/强指针关系,也许还有图像或代码示例?
(请不要只是告诉我“使用boost”或“使用tr1”等。这不是作业,我想学习)。
I have been attempting to write my own weak/strong pointer's but I am not clearly understanding the relationship. Everything I seem to come across does not make it and clear and quite often one doc will contridict what another doc says. Could anyone please explain the weak/strong pointer relationship in detail, with maybe an image or code sample also?
(Please do not just tell me to "use boost" or "use tr1", etc. This is not homework, I want to learn).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
强指针拥有底层原始指针。它的存在可以使原始指针保持活动状态,因此它不能指向已清理的内容。它的破坏可能会导致原始指针的删除。弱指针只知道原始指针,以及它是否仍然有效。它不会通过现有的方式使原始指针保持活动状态,也不能通过清理来使原始指针消失。
要获得更详细的答案,您需要展示您发现的矛盾或您对定义的特定部分的疑问。
A strong pointer owns the underlying raw pointer. Its existence can keep the raw pointer alive, and as a result it can't point to something that's been cleaned up. Its destruction can cause a delete on the raw pointer. A weak pointer only knows the raw pointer, and whether or not it's still valid. It doesn't keep the raw pointer alive by existing and it can't make the raw pointer go away by being cleaned up.
To get a more detailed answer you would need to show the contradictions you've found or the question that you have about a specific part of the definition.