简体   繁体   English

static object 在错误的时间调用构造函数

[英]static object calls constructor at wrong time

I have an opengl batch renderer, which has a static vao, vbo, ebo etc. problem is, int the constructor of those are opengl methods.我有一个 opengl 批处理渲染器,它有一个 static vao、vbo、ebo 等。问题是,它们的构造函数是 opengl 方法。 now, because they are static the opengl methods like glGenBuffers get called before opengl has been initialized.现在,因为它们是 static,所以在初始化 opengl 之前调用了诸如glGenBuffers之类的 opengl 方法。


so you can get a better picture, this is how it looks:这样您就可以获得更好的图片,这就是它的外观:

class renderer2d
{
private:
    static vertex_array vao;
    static vertex_buffer vbo;
    static index_buffer ibo;

public:
    static void draw();
    static GLuint create_quad(glm::vec2 position, glm::vec2 size, GLfloat angle, glm::vec4 color);
}

and int the constructor of eg vao:和 int 例如 vao 的构造函数:

vao()
{
    //some sort of opengl method, that gets called without opengl being initialized
    glGenVertexArrays(1, &id);
}

btw, i dont only want to "solve" the problem while keeping the "static solution", if you have different ideas on how to do this, please tell me顺便说一句,我不仅想在保持“静态解决方案”的同时“解决”问题,如果您对如何做到这一点有不同的想法,请告诉我

One trick is to delay initialisation of the object like this:一个技巧是像这样延迟 object 的初始化:

renderer2d& get_renderer()
{
    static renderer2d renderer;
    return renderer;
}

This method works for any class, it does not require the renderer itself to have static data.此方法适用于任何 class,它不需要渲染器本身具有 static 数据。 The function can also be a static member of the class, as part of the Meyers singleton design. function 也可以是 class 的 static 成员,作为 Meyers Z2ED500E6752961B71DZ 设计的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM