围绕shared_ptr构建对象系统
Building an Object System Around shared_ptr
我正在使用shared_ptr
作为我的垃圾收集器,用于我正在开发的一种编译为c++的玩具语言。我的对象派生自上面的一个公共基类,有字符串和数字,然后有向量和映射。c++方面的所有内容都被包裹在shared_ptr
s中,所以我的容器实际上持有shared_ptr
,这样当它们被破坏时,它们的内容也被破坏了。这个方案工作,但感觉有点奇怪的是,容器是基本对象持有shared_ptr
s。我的设计有缺陷吗?如果是,围绕这种方法的另一种层次结构是什么?
我是这样设置的:
namespace toylang {
class Object;
// main handle type; use this for all object references
// replace with boost::intrusive_ptr or similar if too inefficient
typedef std::shared_ptr<Object> obj;
class Object
{
// whatever
};
class Number : public Object
{
int x;
// etc
};
class Array : public Object
{
std::vector<obj> a;
// etc
}
请注意,该方案中的ToyLang数组是指针的向量,从而提供了语言的引用语义。事实上,这在动态语言中非常常见:Lisp、Python和其他语言都是这样工作的。只要您没有循环引用,shared_ptr
的引用计数将为您提供适当的垃圾收集。
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- CMake-按正确顺序将项目与C运行时对象文件链接
- 使用带有C++对象和标准库容器的插件系统
- 如果在创建对象时创建了 VPTR,那么为什么具有虚拟功能的类的大小在 32 位系统上为 4,在 64 位机器上为 8
- 在C++中,系统如何将这些对象中的每一个与执行程序的窗口相关联?
- 系统C 从字符串对象sc_uint
- 错误 C2440:无法将"系统::D rawing::位图"转换为"系统::对象"
- 学生成绩管理系统 在C++中,表达式必须具有指向对象类型的指针
- Q系统关闭时未销毁的子对象
- 如果堆分配的对象被销毁并且指针被重新分配,Qt的信号和插槽系统会工作吗?
- Qt元对象系统:使用带名称的字符串发出信号
- 面向对象的酒店预订系统设计
- 映射到unordered_map中的对象,它是否被系统初始化
- 面向对象菜单系统
- 缓存对象应该直接从文件系统中读取吗?
- Java - Can调用系统.Gc两次,释放底层对象两次
- 用户可能无法在 Linux 系统上打开共享内存对象的原因
- 围绕shared_ptr构建对象系统
- 检查unix系统中是否加载了共享对象