我们几乎已经在所有代码中使用了boost::shared_ptr
,但是我们仍然有一些使用std::auto_ptr
的孤立情况,包括单例类:
template < typename TYPE >
class SharedSingleton
{
public:
static TYPE& Instance()
{
if (_ptrInstance.get() == NULL)
_ptrInstance.reset(new TYPE);
return *_ptrInstance;
}
protected:
SharedSingleton() {};
private:
static std::auto_ptr < TYPE > _ptrInstance;
};
有人告诉我,这是一个很好的理由,为什么它没有成为一个shared_ptr
,但对于我的生活,我不能理解为什么?我知道在下一个标准中auto_ptr
最终会被标记为折旧,所以我想知道我可以用什么/如何替换这个实现。
此外,您是否还有其他考虑使用auto_ptr
shared_ptr
**?**而不是 shared_ptr shared_ptr
**?**的原因,以及您认为将来迁移到shared_ptr会有什么问题吗?
编辑:
auto_ptr
替换shared_ptr
”的回答,答案是肯定的-但是我会在性能上受到一点影响。auto_ptr
最终被标记为折旧,并且我们转移到std::shared_ptr
时,我们将需要彻底测试我们的代码,以确保我们遵守不同的所有权语义。<代码>G228
发布于 2009-08-04 05:01:58
auto_ptr是我使用的唯一一种智能指针。我使用它是因为我不使用Boost,而且我通常更喜欢我的业务/面向应用程序的类来显式定义删除语义和顺序,而不是依赖于智能指针的集合或单个智能指针。
https://stackoverflow.com/questions/1227379
复制相似问题