我正在为我的游戏制作一个简单的图形引擎。
这是接口部分:
class Texture { ... };
class DrawContext
{
virtual void set_texture(Texture* texture) = 0;
}
这是实现部分:
class GLTexture : public Texture
{
public:
...
GLuint handle;
};
void GLDrawContext::set_texture(Texture* texture)
{
// Check if it's GLTexture, otherwise do nothing.
if (GLTexture* gl_texture = dynamic_cast<GLTexture*>(texture))
{
glBindTexture(GL_TEXTURE_2D, gl_texture->handle);
}
}
在这里使用 dynamic_cast 有意义吗?有没有办法避免它?