C++ How to treat an array of classes as an array of primary types? -
i have code:
class vector3 { public: vector3() : x(values[0]), y(values[1]), z(values[2]) { x = y = z = 0; } float& x; float& y; float& z; private: float[3] values; }; class model { public: vector3 vertices[64]; }; i'm doing vector class because want deal values x, y, z in code, operations need contiguous array of values passed function. whole array of vertices[64] need [x0][y0][z0][x1][y1][z1][x2][y2][z2] etc.
but if this:
//get first address: void* firstaddress = &vertices[0]; //or void* firstaddress = vertices; i don't have contiguous array need (the data messed up), , i'm guessing it's because of pointers have in vector3 class. there way can functionality want? (having single array of float dealing values x,y,z)
firstly, standard doesn't define how references should implemented, they'll occupy actual memory in class pointer members would, ruining contiguous data packing you're hoping for....
if focus more on vertices container, , want x/y/z member access elements in it, try like:
template <size_t n> class vertices { public: class proxy { public: proxy(float* p) : x(p[0]), y(p[1]), z(p[2]) { } float& x; float& y; float& z; }; proxy operator[](size_t n) { return proxy(&d_[n * 3]); } const proxy operator[](size_t n) const { return proxy(&d_[n * 3]); } private: float d_[n * 3]; };
Comments
Post a Comment