So I still dont understand why simply using a class is not an acceptable solution. You want a new type with your own defined data and behaviors... which is exactly what classes are meant for.
The point is that we don't want to have to write identical classes if we can help it.
Here's another example:struct Vector3D { double x, y, z; Vector3D(double x, double y, double z) : x(x), y(y), z(z) { } void print(std::ostream &os) const { os << '(' << x << ',' << y << ',' << z << ')'; } }; struct Point3D { double x, y, z; Point3D(double x, double y, double z) : x(x), y(y), z(z) { } void print(std::ostream &os) const { os << '(' << x << ',' << y << ',' << z << ')'; } };
It would be nice to say "a Point3D works exactly as a Vector3D". A typedef would allow that, but then you won't actually get two separate types.
So, like this?
class UnitOfLength
{
public:
// Add your constructors and overloaded operators here
private:
float mValue;
};
class Inch : public UnitOfLength {};
class Centimeter : public UnitOfLength {};
int main(void)
{
Inch i;
Centimeter c = i; // <--- compile error
return 0;
}