Skip to main content
GameDev.net gamedev.net
🔒 Locked

[c++]float[16]

Started by giugio Mar 6, 2009 at 6:06 PM 2 replies 1.4k views
Original Post
giugio
giugio
Hy. How i create a function that add a float[16] var to a vectorvVect? I would use this format (is a matrix) because i would mantain primitive types for the serialization and i not find how serialize a D3DXMATRIX. thanks.
xylent
xylent
You could define your vector as
vector vVect;

and then insert either like:
float *matrix = new float[16];
// initialize matrix
vVect.push_back(matrix);

or
float matrix[16] = { values };
vVect.push_back((float *)&matrix);

Don't forget to delete the pointers when you are done with the vector though, possibly in the destructor of the class that has this vector as a class member.

I'd suggest you write a Matrix class to encapsulate the data and operations related to it though. You could provide methods in that class to get the pointer to the raw data as well, so it would make no difference and be more convenient to read and write code.
Zahlman
Zahlman
Arrays are not first-class types. You can't really store them like that directly. You can make pointers to new allocations and store the pointers in the vector, but if this is the only reason you're doing it, then that (a) wastes memory, (b) may hurt performance, and (c) defeats the purpose of using the standard library container in the first place. (One slightly better reason to use the pointers is if the "arrays" you wanted to store can be different sizes within the same vector.)

The simplest thing is to wrap the array in some kind of structure. This work has already been done for you.
DevFred
DevFred
I always found it kind of funny that you could not assign arrays directly, but when you put them in a struct, it suddenly works :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.