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?
Consider using a tag struct
For bonus points, have conversion operators/constructors:
If you like to live dangerously you can drop the
explicitkeyword, 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: