D如何管理对象的所有权?例如,我需要表达以下内容:
1.) Allocate an object
2.) Give out references/pointers
3.) If the initially allocated object is deleted,
all references/pointers should become inaccessible.
Run Code Online (Sandbox Code Playgroud)
在C++这是可能的std::shared_ptr+ std::weak_ptr.这在D中也有可能吗?
目前 D 中还没有很好的弱引用实现。
请检查这两个论坛主题:
不正确或“几乎”正确的实现:
最大的问题是您需要一个围绕弱引用的良好 API。Java 和.NET 都有它,但可以说它很糟糕。有充分的理由 - 很难想出一个好的 api!