Array of struct's initialization
Apr 4, 2014 at 11:31am UTC
In a book I'm reading, the author has the following struct:
1 2 3 4
struct VertexPos
{
XMFLOAT3 pos;
};
Where XMFLOAT3 is a structure with x,y,z floating point values within. He declares and initializes an array as the following.
1 2 3 4 5 6
VertexPos vertices[] =
{
XMFLOAT3(0.5f, 0.5f, 0.5f),
XMFLOAT3(3.3f, 5.6f, 3.6f),
XMFLOAT3(-4.5f, 2.2f, 6.4f)
};
It doesn't make sense to me why this is working. The way my mind is thinking about this is that vertices is an array of type VertexPos, so then why can it be initialized with objects of type XMFLOAT3?
Apr 4, 2014 at 12:17pm UTC
VertexPos is a POD.
Even C++98 allows value-initialization of subobjects of PODs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
struct VertexPos
{
double pos;
int y;
};
int main()
{
VertexPos x = { 7.5, 42 };
VertexPos y[] = { 7.5, 42, 3.14, 7 };
return 0;
}
Apr 4, 2014 at 8:20pm UTC
XMFLOAT3 isn't a POD type upon inspection. The way the author talked about it casually as "having 3 float values" caused me to describe it as it was but it's not:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
typedef struct _XMFLOAT3
{
FLOAT x;
FLOAT y;
FLOAT z;
#ifdef __cplusplus
_XMFLOAT3() {};
_XMFLOAT3(FLOAT _x, FLOAT _y, FLOAT _z) : x(_x), y(_y), z(_z) {};
_XMFLOAT3(CONST FLOAT *pArray);
_XMFLOAT3& operator = (CONST _XMFLOAT3& Float3);
#endif // __cplusplus
} XMFLOAT3;
So VertexPos in my original post is not a pod and I still do not understand how the initialization of the original array:
1 2 3 4 5 6 7 8 9 10 11
struct VertexPos
{
XMFLOAT3 pos;
};
VertexPos vertices[] =
{
XMFLOAT3(0.5f, 0.5f, 0.5f),
XMFLOAT3(3.3f, 5.6f, 3.6f),
XMFLOAT3(-4.5f, 2.2f, 6.4f)
};
What am I not understanding here?
Topic archived. No new replies allowed.