弱引用或指针
Weak references or pointers
D 如何管理对象的所有权?例如我需要表达以下内容:
1.) Allocate an object
2.) Give out references/pointers
3.) If the initially allocated object is deleted,
all references/pointers should become inaccessible.
在 C++
中,这可以通过 std::shared_ptr
+ std::weak_ptr
实现。这在 D 中也可以吗?
目前在D中没有很好的弱引用实现
请查看这两个论坛主题:
- http://forum.dlang.org/post/l3dj7btvc@digitalmars.com
- http://forum.dlang.org/post/l3o5n6out@digitalmars.com
不正确或 "almost" 正确的实现:
- https://github.com/D-Programming-Language/phobos/blob/7134b603f8c9a2e9124247ff250c9b48ea697998/std/signals.d
- https://github.com/lycus/mci/blob/f9165c287f92e4ef70674828fbadb33ee3967547/src/mci/core/weak.d
- https://github.com/phobos-x/phobosx/blob/d0cc6b45511465ef1d493b0d7226ccb990ae84e8/source/phobosx/signal.d
- https://bitbucket.org/denis-sh/unstandard/src/cb9a835a9ff5/unstd/memory/weakref.d
最大的问题是你需要一个很好的 API 弱引用。 Java 和 .NET 都有,但可以说它很糟糕。有充分的理由 - 很难想出一个好的 api!
D 如何管理对象的所有权?例如我需要表达以下内容:
1.) Allocate an object
2.) Give out references/pointers
3.) If the initially allocated object is deleted,
all references/pointers should become inaccessible.
在 C++
中,这可以通过 std::shared_ptr
+ std::weak_ptr
实现。这在 D 中也可以吗?
目前在D中没有很好的弱引用实现
请查看这两个论坛主题:
- http://forum.dlang.org/post/l3dj7btvc@digitalmars.com
- http://forum.dlang.org/post/l3o5n6out@digitalmars.com
不正确或 "almost" 正确的实现:
- https://github.com/D-Programming-Language/phobos/blob/7134b603f8c9a2e9124247ff250c9b48ea697998/std/signals.d
- https://github.com/lycus/mci/blob/f9165c287f92e4ef70674828fbadb33ee3967547/src/mci/core/weak.d
- https://github.com/phobos-x/phobosx/blob/d0cc6b45511465ef1d493b0d7226ccb990ae84e8/source/phobosx/signal.d
- https://bitbucket.org/denis-sh/unstandard/src/cb9a835a9ff5/unstd/memory/weakref.d
最大的问题是你需要一个很好的 API 弱引用。 Java 和 .NET 都有,但可以说它很糟糕。有充分的理由 - 很难想出一个好的 api!