C++ C++;:参考和指针问题(关于OpenGL的示例)

C++ C++;:参考和指针问题(关于OpenGL的示例),c++,pointers,opengl,reference,pass-by-reference,C++,Pointers,Opengl,Reference,Pass By Reference,我想加载纹理,然后让它们被多个对象使用。这样行吗 class Sprite { GLuint* mTextures; // do I need this to also be a reference? Sprite( GLuint* textures ) // do I need this to also be a reference? { mTextures = textures; } void Draw( textureNumber

我想加载纹理,然后让它们被多个对象使用。这样行吗

class Sprite
{
    GLuint* mTextures; // do I need this to also be a reference?

    Sprite( GLuint* textures ) // do I need this to also be a reference?
    {
        mTextures = textures;
    }

    void Draw( textureNumber )
    {
        glBindTexture( GL_TEXTURE_2D, mTextures[ textureNumber ] );
        // drawing code
    }
};

// normally these variables would be inputed, but I did this for simplicity.
const int NUMBER_OF_TEXTURES = 40;
const int WHICH_TEXTURE = 10;

void main()
{
    std::vector<GLuint> the_textures;
    the_textures.resize( NUMBER_OF_TEXTURES );

    glGenTextures( NUMBER_OF_TEXTURES, &the_textures[0] );

    // texture loading code

    Sprite the_sprite( &the_textures[0] );
    the_sprite.Draw( WHICH_TEXTURE );
}
类精灵
{
GLuint*mTextures;//我需要它也作为参考吗?
Sprite(GLuint*纹理)//我需要它也作为参考吗?
{
纹理=纹理;
}
无效绘制(纹理枚举器)
{
glBindTexture(GL_TEXTURE_2D,mTextures[TextureEnumber]);
//绘图代码
}
};
//通常这些变量会被输入,但我这样做是为了简单。
const int NUMBER_OF_纹理=40;
const int,其中_纹理=10;
void main()
{
std::向量_纹理;
调整大小(纹理的数量);
glGenTextures(纹理的数量和纹理[0]);
//纹理加载代码
精灵精灵精灵(&U纹理[0]);
绘制(哪种纹理);
}
有没有一种不同的方法可以让我做到这一点,即使它能起作用


谢谢。

那种特殊情况应该行得通。但是,一旦“_纹理”超出范围,Sprite持有的指针就会失效。即使它是一个参考,那也是一样的。
在这种情况下,我建议您将std::vector放在Sprite类中,并由该类实例拥有和管理。

这种特殊情况应该有效。但是,一旦“_纹理”超出范围,Sprite持有的指针就会失效。即使它是一个参考,那也是一样的。
在这种情况下,我建议您将std::vector放在Sprite类中,并由该类实例拥有和管理。

我想知道为什么Draw不能只引用它正在绘制的对象

class Sprite
{
public:

    Sprite()
    {
    }

    void Draw( GLuint & texture )
    {
        glBindTexture( GL_TEXTURE_2D, texture );
        // drawing code
    }
};
  • Sprite是一种以特定方式绘制图形的类型
  • GLuint是一种被绘制的类型
这里可能存在多态性: -您有不同的绘制算法 -在绘制的不同类型的对象中存在多态(虚拟)方法

因此Draw可能是一个虚拟方法,GLuint可能是一个抽象基类,在这种情况下,实际的向量不是对象,而是指向不同类型对象的指针

当然,您应该将对象的绘制方式与对象的存储方式解耦,因此在绘图类中存储向量,甚至传入假定它们位于某种数组中的指针都不是一个好主意


顺便说一下,main应该返回int而不是void。

我想知道为什么Draw不能只引用它正在绘制的对象

class Sprite
{
public:

    Sprite()
    {
    }

    void Draw( GLuint & texture )
    {
        glBindTexture( GL_TEXTURE_2D, texture );
        // drawing code
    }
};
  • Sprite是一种以特定方式绘制图形的类型
  • GLuint是一种被绘制的类型
这里可能存在多态性: -您有不同的绘制算法 -在绘制的不同类型的对象中存在多态(虚拟)方法

因此Draw可能是一个虚拟方法,GLuint可能是一个抽象基类,在这种情况下,实际的向量不是对象,而是指向不同类型对象的指针

当然,您应该将对象的绘制方式与对象的存储方式解耦,因此在绘图类中存储向量,甚至传入假定它们位于某种数组中的指针都不是一个好主意


顺便说一句,main应该返回int而不是void。

既然纹理id不变,那么在
Sprite
中使用
GLuint
值,而不是存储数组ptr并选择要绘制的Sprite如何

看起来更简单,不必担心范围问题


除非您需要在应用程序退出之前调用glDeleteTextures,否则我建议您创建一个TextureMgr类或一些可以一次性解决此问题的东西。;)

既然纹理id不变,那么在
Sprite
中使用
GLuint
值,而不是存储数组ptr并选择要绘制的Sprite如何

看起来更简单,不必担心范围问题

除非您需要在应用程序退出之前调用glDeleteTextures,否则我建议您创建一个TextureMgr类或一些可以一次性解决此问题的东西。;)

  • 是的,这样行
  • 无需将它们作为引用:您存储/传递指针的副本(速度很快),并且不打算在外部更改此指针
  • 有许多不同的方法可以做到这一点,正确的方法取决于您的其他代码需求 e、 g.可以使用纹理的全局实例:

    textures.cpp:

    static std::vector load_once_textures();
    std::vector<GLuint> const& get_textures()
    {
        static std::vector<GLuint> const the_textures = load_once_textures();
        return the_textures;
    }
    
    std::vector load_once_textures()
    {
        // loading
    }
    
    static std::vector load_once_textures();
    std::vector const&get_纹理()
    {
    静态std::vector const the_textures=load_once_textures();
    返回_纹理;
    }
    std::向量加载一次纹理()
    {
    //装载
    }
    
    纹理

    std::vector<GLuint> const& get_textures();
    
    std::vector const&get_textures();
    
    这是一种简单且足够安全的方法,因为纹理将加载一次,并且加载不会出现静态初始化顺序模糊的问题

  • 是的,这样行
  • 无需将它们作为引用:您存储/传递指针的副本(速度很快),并且不打算在外部更改此指针
  • 有许多不同的方法可以做到这一点,正确的方法取决于您的其他代码需求 e、 g.可以使用纹理的全局实例:

    textures.cpp:

    static std::vector load_once_textures();
    std::vector<GLuint> const& get_textures()
    {
        static std::vector<GLuint> const the_textures = load_once_textures();
        return the_textures;
    }
    
    std::vector load_once_textures()
    {
        // loading
    }
    
    static std::vector load_once_textures();
    std::vector const&get_纹理()
    {
    静态std::vector const the_textures=load_once_textures();
    返回_纹理;
    }
    std::向量加载一次纹理()
    {
    //装载
    }
    
    纹理

    std::vector<GLuint> const& get_textures();
    
    std::vector const&get_textures();
    

    这是一种简单且足够安全的方法,因为纹理将加载一次,并且加载不会出现静态初始化顺序不明确的问题

    只是一个注释。如果您正在定义(vs声明)