テクスチャを読み込んで複数のオブジェクトで使用したいと思います。これは効果がありますか?C++:リファレンスとポインタの質問(OpenGLに関する例)
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);
}
これは動作しても違う方法がありますか?
ありがとうございました。
だけではありませんので、それは単純なアプローチと十分に安全です注意。参照として何かを定義(宣言)する場合は、通常、デザインに何か問題があることを意味します。 – Falmarri
@Falmarri、どういう意味ですか?あなたはクラスフィールドを参照、または参照されているローカル変数を持つことが間違っていることを示唆していますか? – Kos
@Kos:私はクラスフィールドを参照として定義することを話しています。 – Falmarri