c++ - 是否适合为每个“类型”创建一个带有构造函数重载的模板类
问题描述
我是使用模板的新手。
为各种实际类型使用构造函数重载创建模板类是否合适?
例如:
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,这就是我所指的,一个是精灵,另一个是形状,但具有通用功能,但也具有专有功能,例如 setColor 或 setTexture 函数。我的意思是,将 T 的第二个构造函数作为精灵调用是没有逻辑意义的:
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>
使用模板来分解一些通用代码,但是这两个类都有特定的接口。
然后,IMO,在接口方法中提供不使用是不好的。
std::vector
提供了一些对类型有预先要求的方法T
(v.resize(count)
需要默认可插入)。但它是通用类,组合会很大,所以可以接受 IMO。
我认为不常用的方法应该从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 个新类
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) { /*..*/} // ... };
推荐阅读
- macos - Mac 控制中心“正在播放”信息未显示超过 1 小时的持续时间
- hadoop - Hadoop fs复制命令?
- c# - 是否有返回 MVC .Net Framework 中返回的最后一个视图的函数
- taleo - Taleo Connect 客户端脚本
- jinja2 - markdown 模板中 jinja2 中的变量使用
- python-3.x - 将 pandas 数据框列从列表展平到它们自己的特定列
- java - 难以将 Google Guava 地图元素添加到 java 中的数组
- java - SetAdapter 空指针对象异常
- mongodb - 如何在mongo聚合期间复制文档?
- angular - 为生产“在 Azure DevOps”上构建 Angular 项目时出错