相关疑难解决方法(0)

在VC++ 8中替换auto_ptr

std::auto_ptr在VC++ 8(这是我们在工作中使用的)中被破坏了.我对它的主要抱怨是,它允许auto_ptr<T> x = new T();,当然导致可怕的崩溃,而错误地做的很简单.

从stackoverflow上的另一个问题的答案:

请注意,Visual Studio 2005中std :: auto_ptr的实现严重受损. http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=98871 http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=101842

我想用

  • boost::scoped_ptr,对于不应该通过所有权的指针.
  • boost::shared_ptr,用于容器中的指针以及需要它们的其他地方.
  • std::auto_ptr,对于应该/可以通过所有权的指针.

但是因为std::auto_ptr对我而言是破碎的,我想知道什么是最好的方法:

  • std::auto_ptr网上的东西替换.像这样一个从拉尼Sharoni(还没有尝试过呢).
  • boost::shared_ptr改用.当然会工作,虽然会有一些我不关心的小开销.但我想用它auto_ptr来表示指针的意图.(有关方法的投票,请参阅答案.)
  • 我永远不需要在实践中通过所有权,所以我不应该担心这一点.

更新:这就是我所做的:我复制了Rani Sharoni提到的auto_ptr实现.从这里开始.

做了一些小测试:

class T
{
public:
    T() {
        OutputDebugStringA("T\n");
    };
    ~T() {
        OutputDebugStringA("~T\n");
    };
};

{
    fix::auto_ptr<T> x(new T); // This just works.
}
{
    fix::auto_ptr<T> x = (new T); // Doesn't compile. …
Run Code Online (Sandbox Code Playgroud)

c++ smart-pointers

9
推荐指数
1
解决办法
2025
查看次数

标签 统计

c++ ×1

smart-pointers ×1