我刚开始使用模板。
对各种实际类型使用构造函数重载来创建模板类是否合适?
例如:
template <typename T>
class A
{
A(..will receive paramaters for a specific type T);
A(..will receive DIFFERENT params for a different type T);
}
代码将编译,因为每个实例化的所有功能都有效,但从逻辑上(和实际上)来说,每个构造函数将根据我实际使用的类型进行调用。
这很符合我的目的,但是这是正确的“编程思维”吗?
更具体地说 - 使用 SFML,这就是我所指的, 一个是 Sprite ,另一个是形状,但具有常见功能但也具有排他性,例如 setColor 或 setTexture 函数。我的意思是,将 T 作为 Sprite 调用第二个构造函数是没有逻辑意义的:
template <typename T>
class GameObject {
public:
GameObject(sf::Texture& texture, sf::Vector2f pos)
{
m_drawable.setTexture(texture);
m_drawable.setOrigin(m_drawable.getGlobalBounds().width / 2, m_drawable.getGlobalBounds().height / 2);
m_drawable.setPosition(pos);
}
GameObject(sf::Color color, sf::Vector2f pos)
{
m_drawable.setColor(color);
m_drawable.setPosition(pos);
}
private:
T m_drawable;
};
最佳答案
据我了解,您只会有 GameObject<Sprite>
和GameObject<Shape>
并使用模板来分解一些通用代码,但这两个类都有特定的接口(interface)。
然后,IMO,在接口(interface)中提供不使用的方法是不好的。
std::vector
提供了一些对类型 T
有前置要求的方法( v.resize(count)
需要默认可插入)。但它是通用类,组合会很大,所以在我看来是可以接受的。
我认为应该从 GameObject<T>
中删除非常规方法
在 C++20 中,有
requires
允许有条件地“删除”方法:template <typename T> class GameObject { public: GameObject(sf::Texture&, sf::Vector2f pos) requires(std::is_same_v<Sprite, T>); GameObject(sf::Color, sf::Vector2f pos) requires(std::is_same_v<Shape, T>); // ... };
SFINAE 是以前标准的替代品。
您可能有 2 个新类(class)
GameSprite
/GameShape
它使用/继承已清理的GameObject<T>
作为实现细节。或者,您可以使用 (
friend
) 自由函数来实现不同的行为template <typename T> class GameObject { GameObject(T&& t) : m_drawable(std::move(t)){} public: friend GameObject<Sprite> GameSprite(sf::Texture& texture, sf::Vector2f pos) { Sprite sprite; sprite.setTexture(texture); sprite.setOrigin(sprite.getGlobalBounds().width / 2, sprite.getGlobalBounds().height / 2); sprite.setPosition(pos); return GameObject<Sprite>{std::move(sprite)}; } friend GameObject<Shape> GameShape(sf::Color color, sf::Vector2f pos) { /*..*/} // ... };
关于c++ - 对于每个 "type"有一个带有构造函数重载的模板类是否合适,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62398154/