我喜欢Boost的smart_ptr功能和转换和从shared_ptr
和weak_ptr
,但由于引用计数不包含在指出的类本身,下面的代码不起作用(和它不应该)。嵌入引用计数与提升shared_ptr
A *a = new A;
shared_ptr<A> aPtr1(a);
{
shared_ptr<A> aPtr2(a);
// The reference counts of aPtr1 and aPtr2 are both 1.
} // At this point, `a` is destructed by aPtr2.
aPtr1->foo(); // And... SIGTERM
我相信JUCE框架有这个功能。 [ReferenceCountedObject
和ReferenceCountedObjectPtr
] 但是,我宁愿使用Boost来申请我的应用程序。是否有可能允许Boost smart_ptrs在指出的类中查找引用计数而不是私有的实例?
看来,我想要的是一个计数的身体/附加智能指针,来自[this](http://www.boost.org/community/counted_body.html)文章。 – Vortico
只需在程序中混合使用智能指针和普通指针,选择一种方法并坚持下去。 – chill
在所有情况下,我都使用'shared_ptr's和'weak_ptr's,但构造函数只能访问原始指针'this'。目前,我无法在构造函数中初始化字段,因此在'a'和'a-> b'之间存在双向关系。 – Vortico