自定义弱/强引用指针
我正在创建自己的弱/强引用指针关系的实现,但我对配置感到困惑。当我有一个包含强引用的类,并且我想将强指针设置为另一个具有弱引用的类时,我是否应该传入weak_ref指针的指针?
如果有人可以看一下这段代码并让我知道我会感激它,如果您发现任何其他问题,请告诉我。我将这三个文件放入键盘文件中,这样这个页面就不会变得麻烦。
WeakReference.h : http://codepad.org/nNtRk4vO
StrongReference.h : http://codepad.org/MGi0fZ4J
请不要将其变成“使用 boost、使用 std、使用 tr1”参数,我正在寻求帮助在此代码上,不使用其他东西。
I am creating my own implementation of a weak/strong reference pointer relationship and I am confused to the configuration. When I have a class that contains the strong reference, and I want to set the strong pointer to another class that has a weak reference, should I be passing in the pointer of a weak_ref pointer?
If someone could please take a look at this code and let me know I will appriciate it, also if you notice any other issues please let me know. I put the three files into codepad files so this page does not become cumbersome.
WeakReference.h : http://codepad.org/nNtRk4vO
StrongReference.h : http://codepad.org/MGi0fZ4J
Please do not turn this into a "use boost, use std, use tr1" argument, I am looking for help on this code, not using something else.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我所见过的以及我所从事的使用强引用和弱引用概念的实现都使用两个计数。有时,一个计数是弱引用的数量,另一个计数是强引用的数量。其他时候,其中一个计数代表强引用+弱引用的总和。有时还有其他方案。但我还没有看到像你的那样只有一项计数的实现。
如果您为strong_ref 和weak_ref 的行为准确地编写一个规范,也许会有所帮助。我发现编写规范的行为实际上可以帮助调试实现。它迫使你思考输入、输出、极端情况、无效情况等。
The implementations that I have seen, and that I have worked on, that use the concepts of strong and weak reference, all use two counts. Sometimes one count is the number of weak references and the other is the number of strong. Other times one of the counts represents the sum of strong + weak references. Sometimes there are other schemes. But I have not yet seen an implementation with only one count as yours seems to have.
Perhaps it would help if you wrote a specification for exactly what the behavior for strong_ref and weak_ref are supposed to be. I find that the act of writing a specification can actually aid in the debugging of the implementation. It forces you to think about inputs, outputs, corner cases, invalid cases, etc.