我不了解有关如何使用make_shared和allocate_shared初始化共享数组和指针的boost文档:
shared_ptr<int> p_int(new int); // OK
shared_ptr<int> p_int2 = make_shared<int>(); // OK
shared_ptr<int> p_int3 = allocate_shared(int); // ??
shared_array<int> sh_arr(new int[30]); // OK
shared_array<int> sh_arr2 = make_shared<int[]>(30); // ??
shared_array<int> sh_arr3 = allocate_shared<int[]>(30); // ??
Run Code Online (Sandbox Code Playgroud)
我正在尝试学习正确的语法来初始化上述注释为的变量 // ??
allocate_shared的用法与一样make_shared,只是您将分配器作为第一个参数传递。
boost::shared_ptr<int> p_int(new int);
boost::shared_ptr<int> p_int2 = boost::make_shared<int>();
MyAllocator<int> alloc;
boost::shared_ptr<int> p_int3 = boost::allocate_shared<int>(alloc);
Run Code Online (Sandbox Code Playgroud)
没有的make功能boost::shared_array,因此唯一的方法是手动分配内存:
boost::shared_array<int> sh_arr(new int[30]);
Run Code Online (Sandbox Code Playgroud)
但是boost::make_shared等支持数组类型-未知大小之一,或固定大小之一-在两种情况下boost::shared_ptr都返回a:
boost::shared_ptr<int[]> sh_arr2 = boost::make_shared<int[]>(30);
boost::shared_ptr<int[30]> sh_arr3 = boost::make_shared<int[30]>();
Run Code Online (Sandbox Code Playgroud)
请注意,std::shared_ptr目前不支持数组。
| 归档时间: |
|
| 查看次数: |
2121 次 |
| 最近记录: |