Passing an int to a function, then using that int to create an array

后端 未结 1 343
走了就别回头了
走了就别回头了 2021-01-23 07:15

I\'m trying to create a textureLoader class for my openGL project and I can\'t initialize an array of textures inside of my class constructor because the the array won\'t accept

1条回答
  •  闹比i
    闹比i (楼主)
    2021-01-23 07:41

    Your second options is close, you can get at the underlying array of the vector by calling .data()

    myConstructor(int num)
    {
        std::vector  textures(num);
        glGenTextures(num, textures.data());
    }
    

    Assuming glGenTextures has a signature like

    void glGenTextures(int, GLuint*)
    

    I don't know much about this function, but be careful who owns that array. The vector will fall out of scope after your constructor so I'm hoping that glGenTextures will copy whatever it needs. Otherwise if the array needs to persist

    myConstructor(int num)
    {
        GLuint* textures = new GLuint[num];
        glGenTextures(num, textures);
    }
    

    But then I'm not sure who is supposed to clean up that memory.

    0 讨论(0)
提交回复
热议问题