自定义弱/强引用指针

发布于 2024-10-22 08:52:53 字数 466 浏览 5 评论 0原文

我正在创建自己的弱/强引用指针关系的实现,但我对配置感到困惑。当我有一个包含强引用的类,并且我想将强指针设置为另一个具有弱引用的类时,我是否应该传入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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可可 2024-10-29 08:52:53

我所见过的以及我所从事的使用强引用和弱引用概念的实现都使用两个计数。有时,一个计数是弱引用的数量,另一个计数是强引用的数量。其他时候,其中一个计数代表强引用+弱引用的总和。有时还有其他方案。但我还没有看到像你的那样只有一项计数的实现。

如果您为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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文