I'd like to now if it's possible, and eventually how it's possible, to implement a class in a special way so that you can create a vector of that class and, after having filled it, iterate through the classes attributes without iterating the vector.
This is an example:
class Vertex3f
{
public:
union
{
float vec[3];
struct
{
float x, y, z;
};
};
public:
Vertex3f();
~Vertex3f();
...
}
Then create a std::vector
of this class, fill it and access all the class' instances attributes:
Vertex3f v1, v2;
v1.x = 10.0f;
v1.y = 0.0f;
v1.z = 5.0f;
v2.x = 8.0f;
v2.y = -5.0f;
v2.z = 3.0f;
// Create and fill the vector
std::vector<Vertex3f> vec;
vec.push_back(v1);
vec.push_back(v2);
// Get all the vertices data in one go
// This is the tricky part I'm not sure it can be done
size_t size = vec.size() * (sizeof(float) * 3);
float* vertices = new float[size];
memcpy(vertices, &vec[0].x, size);
I now that the Bullet Physics library does something like this for performance reason and I'd like to do the same, but I don't understand how.