How should the following cases be handled:
I have some geometrical storage, which is a template from vertex type.
template <typename T> struct Geometry {
std::vector<T>& GetVertices() { ... }
const void* RawVertices() const { ... }
}
This works fine, unless I want to store different types of geometries (for instance, Geometry<Vertex1> g1 and Geometry<Vertex2> g2 in one container.
Is this possible?
Or how should I implement geometry storage (where I can store and retrieve different types of geometries using one container) or maybe somehow map T type to Geometry<T> type?
Any advices?
Thank you.
Since a container is tied to one type of data it can contain, you could create a class
GeometryBasefrom which allGeometry<T>are derived and then storeGeometryBasepointers in your container.Edit:
At some point you will have to decide which type of vertex container you want to get (my approach) or what you want to do with a vertex (Vijay Mathew’s approach) and then you’ll have to dynamic_cast<> in order to get access to the derived class methods.
Another suggestion:
If the types are as different as your describe in your comments, it might actually be better to treat them as different types.
For example, you could create a separate container for each
Geometry<>template instance.If you have functions that operate on one kind of geometry (using
Vertex1::GetPos(), to use your example) or the other (Vertex2::GetUV()) then these functions are probably implemented quite differently and thus deserve to be separate functions expecting diferent types of parameters.