Best Approach For This?
I'm writing a simple 2d vector object. It will have x and y components,
and length, cross product, etc. methods. The thing is, I want to have the
structure to have many possible types (char, int, float, double, etc.). I
was wondering what would be the best choice, design wise, to interact with
the object? Here's what I'm currently considering:
1. Have the user pass the vector object to specialized functions, like:
Vector2Dt_Dot(Vec2Dt& vector1, Vec2Dt& vector2);
where 't' is the type of the vector. However, the problem with this
approach is that it disallows different types from interacting with each
other, so I couldn't say calculate the dot product of a float vector2d and
a double vector2d. A second approach, and what I'm leaning towards:
2. Have the user pass the vector object(s) as void pointers, along with
their types, like:
Vector2D_Dot(void* vector1, unsigned vector1_type, void* vector2, unsigned
vector2_type);
obviously this approach is more compact api-wise and also solves the above
problem, but at the cost of a few extra parameters and type safety.
There may be other solutions that I'm not aware of, however these are the
ones I'm currently considering. What do you feel is the best approach to
this?
No comments:
Post a Comment