C++语义类型包装
我有一个数据类型,例如 class Vector3
。现在我需要创建几个与 Vector3
具有相同接口的类,但具有更高级别的语义(例如:Position
、Velocity
)。使用 typedef 是不够的,因为我需要这些类型是不同的,以便它们可以用于重载。在 C++0x 中,我可能可以使用构造函数继承:
struct Position: public Vector3 {
using Vector3::Vector3;
};
这会有什么问题吗?有没有更好的办法呢?是否可以在不使用 C++0x 功能且不必显式编写所有 Vector3
构造函数的情况下完成此操作?
I have a data type for example class Vector3
. Now I need to create several classes with same interface as Vector3
, but with higher level semantic (for example: Position
, Velocity
). Using typedef
is not enough, because I need these types to be distinct so that they can be used for overloading. In C++0x I could probably use constructor inheritance:
struct Position: public Vector3 {
using Vector3::Vector3;
};
Could there be any problems with this? Is there any better way to do it? Is it possible to do it without using C++0x features and not having to explicitly write all Vector3
constructors?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
考虑使用标记结构
为了获得奖励积分,请使用转换运算符/构造函数:
如果您喜欢危险地生活,则可以删除
explicit
关键字,或启用隐式转换运算符。这将具有能够启用混杂运算符解析的“好处”,如下所示:我建议(相反)为这样的情况定义显式运算符(自由函数) :)
这样你的类模型就可以反映你的类的语义。
C++0x 可能包含启用显式转换运算符的内容,IIRC:
Consider using a tag struct
For bonus points, have conversion operators/constructors:
If you like to live dangerously you can drop the
explicit
keyword, or enable the implicit conversion operator. This would have the 'benefit' of being able to enable promiscuous operator resolutions like so:I'd recommend (instead) to define explicit operators for cases like this (free functions:)
That way your class model reflects the semantics of your classes.
C++0x would possibly contain things to enable explicit conversion operators, IIRC:
当我需要不同但相似的类型时,我通常使用虚拟模板参数,例如(即兴的,不受编译器的脏手影响)
这可能是也可能不是您需要的。
干杯&呵呵,
When I've needed distinct-but-similar types I've generally used a dummy template parameter, like (off the cuff, untouched by compiler's dirty hands)
This may or may not be what you need.
Cheers & hth.,