English 中文(简体)
将自动用户转换为共享用户(_ptr)
原标题:converting a auto_ptr to a shared_ptr

How can I change an std::auto_ptr to a boost::shared_ptr? Here are my restrictions: 1. I am using an API class, lets call it only_auto that returns these pointers 2. I need to use the call in auto_only 3. My semantics involves sharing so I do need to use a shared_ptr) 4. In class only_auto operator = is private to prevent coping 5. Also a only_auto object must be made by a cloning call std::auto_ptr creat_only_auto();

我知道明确的共享_pdr( std: auto_ pdr & amp; r) 模板; 但我在这种情景下如何使用它?

超级简化代码示例 :

    #include <iostream>
    #include <memory>
    #include <boost/shared_ptr.hpp>

    using namespace std;

    class only_auto
    {
      public:
      static auto_ptr<only_auto> create_only_auto();
      void func1();
      void func2();
      //and lots more functionality

      private:
      only_auto& operator = (const only_auto& src);
    };

    class sharing_is_good : public only_auto
    {
      static boost::shared_ptr<only_auto> create_only_auto()
      {
        return boost::shared_ptr (only_auto::create_only_auto()); //not the correct call but ...
      }

    };

    int main ()
    {
       sharing_is_good x;

       x.func1();
    }
问题回答

shared_ptr 构建器被宣布为:

template<class Other>
shared_ptr(auto_ptr<Other>& ap);

注意它需要一个非默认值引用。 它这样做是为了能够强制释放该对象的 < code> auto_ ptr 所有权 。

因为它需要非默认值引用, 您不能用 rvalue 调用此成员函数, 这就是您想要做的 :

return boost::shared_ptr(only_auto::create_only_auto());

您需要将 only_auto::: create_ only_auto() 的结果存储到变量中, 然后将该变量传送到 共享_ ptr 构建器 :

std::auto_ptr<only_auto> p(only_auto::create_only_auto());
return boost::shared_ptr<only_auto>(p);

3. 我的语义包含共享, 所以我需要使用共享_ ptr)

最有效的自动- ptr 使用源代码与 std: unique_ ptr 兼容 : unique_ ptr, 所以您可能想要查看转换到该源代码。 如果一切都构建好, 那么您就安全了 。 (如果您尚未重新使用 typedef, 您可能想要移动到 typedef 上, 这样您就可以轻松地在未来更改该类型 。) 如果您有编译错误, 您可能会在代码中出现错误, 您以前使用无效的自动- ptr 的错误 。

我认为你应该只看向Std: 共享_ ptr : 在你用独特_ ptr 验证了事物后, 共享_ ptr 表示你确实需要共享所有权( 通常你只能使用独有的所有权和非拥有的指针 ) 。

我觉得

return boost::shared_ptr<only_auto>(only_auto::create_only_auto().release());

做戏法应该做戏法





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签