非模板类的模板化构造
我有一个具有公共成员的类,但需要基于枚举以有限数量的方式构造。每种类型在编译时都是已知的,因此我认为模板在这里有意义。我知道我可以通过构造函数专门化来解决这个问题,例如:
enum SensorTypes{GPS,Radar,ShaftEncoder};
template<SensorTypes>
class Sensor
{
public:
Sensor(unsigned char* rawdata){//Format of rawdata depends on SensorTypes};
private:
double speed;
double time;
}
template<> Sensor<GPS>::Sensor(unsigned char* rawdata){speed = (double)rawdata[0];}
问题是我有遗留代码,它必须接受 Sensor
类而不是 Sensor
等。我怎样才能实现类似的目标编译时构造,同时保持单个类类型。
I have a class that has common members but needs to be constructed in a finite number of ways based on an enumeration. Each type is known at compile time, so I am thinking templates make sense here. I know I can solve this with constructor specialization, e.g.:
enum SensorTypes{GPS,Radar,ShaftEncoder};
template<SensorTypes>
class Sensor
{
public:
Sensor(unsigned char* rawdata){//Format of rawdata depends on SensorTypes};
private:
double speed;
double time;
}
template<> Sensor<GPS>::Sensor(unsigned char* rawdata){speed = (double)rawdata[0];}
The problem is I have legacy code which must accept Sensor
classes not Sensor<GPS>
etc. How can I achieve similar compile time construction while maintaining a single class type.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
乍一看这似乎很简单,只需在
Sensor
类中使用模板化构造函数即可。但此时很容易看出“类型参数”实际上是在描述原始数据,而不是其他任何东西。
所以实际上,应该输入 rawdata 参数。
使原始数据的类型更加严格还可以帮助您避免错误,例如雷达原始数据被视为 GPS 原始数据。
从设计角度来看,这分为原始数据提供者和原始数据解释器(
Sensor
类显然是一个原始数据解释器)。问题暗示了这种设计,但如果可能是的话,那么将解释知识移近数据源可能是有益的。
即,将雷达数据的解释从
Sensor
构造函数和类中移出,并转移到携带雷达原始数据的类中。干杯&呵呵,,
This seems simple enough at first, just use a templated constructor in the
Sensor
class.But at this point it's easy to see that the "type-argument" is really describing the rawdata, and nothing else.
So really, it should be the rawdata argument that should be typed.
Making the rawdata more strictly typed also helps you avoid foul-ups were e.g. radar rawdata is treated as GPS rawdata.
Design-wise, this is partitioning into rawdata providers and rawdata interpreters (the
Sensor
class is evidently a rawdata interpreter).That design is implied by the question, but if may be that it could be beneficial to move the interpretation knowledge closer to the data sources.
I.e., to move the interpretation of e.g. radar data out of the
Sensor
constructor and class, and into the class carrying radar rawdata.Cheers & hth.,
根据您的特定需求,您也许能够创建非模板基类并派生特定于模板的版本,使用虚拟方法来选择正确的行为。
Depending on your specific needs, you may be able to create a non-template base class and derive the template-specific version, using virtual methods to select the right behavior.
这看起来很简单,只需让您的模板类从现有的 Sensor 类派生即可。
This seems simple enough, just have your template class derive from the existing
Sensor
class.