c++ - 初始化 std::unique_ptr 作为原始数组指针初始化

标签 c++ arrays initialization std unique-ptr

我目前有一个小型 OpenGL 项目,其中我在堆上创建一个数组,

float* vertices = new float[48] {
     0.5f,  0.5f, 0.5f,  1.0f, 0.0f, 0.0f,  // front top right,    0
     0.5f, -0.5f, 0.5f,  0.0f, 1.0f, 0.0f,  // front bottom right, 1
    -0.5f, -0.5f, 0.5f,  0.0f, 0.0f, 1.0f,  // front bottom left,  2
    -0.5f,  0.5f, 0.5f,  0.4f, 0.8f, 0.2f,  // front top left,     3

     0.5f,  0.5f, -0.5f, 1.0f, 0.0f, 0.0f,  // back top right,     4
     0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.0f,  // back bottom right,  5
    -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 1.0f,  // back bottom left,   6
    -0.5f,  0.5f, -0.5f, 0.4f, 0.8f, 0.2f   // back top left,      7 
};

我预计会将其扩展到数百或一千个值。因此,我想使用 std::unique_ptr。我查看了文档,似乎不可能像原始数组指针一样初始化 std::unique_ptr 。尽管如此,我尝试过写这样的东西,

std::unique_ptr<float[]> vertices(new float[48])  {
    0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f,     // front top right,    0
    0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 0.0f,    // front bottom right, 1
   -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,    // front bottom left,  2
   -0.5f, 0.5f, 0.5f, 0.4f, 0.8f, 0.2f,     // front top left,     3

    0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f,    // back top right,     4
    0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.0f,   // back bottom right,  5
   -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 1.0f,   // back bottom left,   6
   -0.5f, 0.5f, -0.5f, 0.4f, 0.8f, 0.2f     // back top left,      7 
};

无法编译。

是否可以像原始指针数组一样初始化 unique_ptr 数组?

我考虑过使用std::vector,然后使用unique_ptr“捕获”它,尽管我还没有尝试过。

最佳答案

除非有非常具体的原因,否则您应该使用 std::vector 。这是动态分配数组的标准类型。 my_vector.data()返回指向数据的指针,因此它与 C api(例如 OpenGL)兼容。

// on the heap
std::vector<float> vertices = {
     0.5f,  0.5f, 0.5f,  1.0f, 0.0f, 0.0f,  // front top right,    0
     0.5f, -0.5f, 0.5f,  0.0f, 1.0f, 0.0f,  // front bottom right, 1
    -0.5f, -0.5f, 0.5f,  0.0f, 0.0f, 1.0f,  // front bottom left,  2
    -0.5f,  0.5f, 0.5f,  0.4f, 0.8f, 0.2f,  // front top left,     3

     0.5f,  0.5f, -0.5f, 1.0f, 0.0f, 0.0f,  // back top right,     4
     0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.0f,  // back bottom right,  5
    -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 1.0f,  // back bottom left,   6
    -0.5f,  0.5f, -0.5f, 0.4f, 0.8f, 0.2f   // back top left,      7 
};

如果你想要 vector (如果你需要调整大小等),只需使用 std::vector<float>作为类型而不是 std::array<float, 48>


现在,如果你想修复你的语法,但仍然使用动态内存和指针(我建议不要,甚至更多,因为它是针对 OpenGL 顶点,并且你提前知道大小),只需替换类型,用新数组构造它里面:

std::unique_ptr<float[]> vertices(new float[48] {
     0.5f,  0.5f, 0.5f,  1.0f, 0.0f, 0.0f,  // front top right,    0
     0.5f, -0.5f, 0.5f,  0.0f, 1.0f, 0.0f,  // front bottom right, 1
    -0.5f, -0.5f, 0.5f,  0.0f, 0.0f, 1.0f,  // front bottom left,  2
    -0.5f,  0.5f, 0.5f,  0.4f, 0.8f, 0.2f,  // front top left,     3

     0.5f,  0.5f, -0.5f, 1.0f, 0.0f, 0.0f,  // back top right,     4
     0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.0f,  // back bottom right,  5
    -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 1.0f,  // back bottom left,   6
    -0.5f,  0.5f, -0.5f, 0.4f, 0.8f, 0.2f   // back top left,      7 
});

std::unique_ptr type 有一个构造函数,它采用指向类型 T 的指针。构造函数只是显式的,因此只能使用直接初始化。

这是一个live example具有所有构造函数类型。

关于c++ - 初始化 std::unique_ptr 作为原始数组指针初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56761281/

相关文章:

c++ - 指针和引用之间的底层区别是什么?

c++ - 请求非标量类型

vb.net - VB.Net 中类成员的这两种初始化方法有什么区别?

c++ - 使用迭代函数调用初始化 std::vector

c++ - Arduino电路没有响应

c++ - OpenCV C++中的微小星球全景图

java - 如何在java中声明调用二维数组?

php - 有没有办法在 PHP 的 foreach 循环中以特定值开始?

javascript - 数据表扩展 excel 选项不适用于分页

c++ - 如何使用初始化列表来选择要使用的构造函数?