I have a struct array like this:
struct VERTEX_FMT
{
float x, y, z, u, v;
const static DWORD FVF = (D3DFVF_XYZ | D3DFVF_TEX1);
};
VERTEX_FMT vertices[] = {
{-1.0f, -1.0f, 0.0f, 0.0f, 0.0f},
{-1.0f, 1.0f, 0.0f, 0.0f, 1.0f},
{ 1.0f, -1.0f, 0.0f, 1.0f, 0.0f},
{ 1.0f, 1.0f, 0.0f, 1.0f, 1.0f},
};
Is there an easy to way assign the struct array a new value in C++.
Only if you wrap the array in another
struct. Arrays are pretty broken in C, and C++ decided thatstd::vectorwas sufficient, and that any attempt to fix C style arrays either wouldn’t go far enough to make a difference, or would break compatibility to a point where you couldn’t talk of C style arrays any more.For POD arrays like yours,
memcpyis by far the simplest solution. For more complicated cases, and even most cases of arrays ofstructs like yours, you should probably consider usingstd::vectorinstead. Or a mixture: use the C style arrays for static, initialized arrays such as the one you show, so that the compiler will count the number of elements for you, but usestd::vectorfor anything which isn’t completely initialized in the definition, or which is the target of an assignment. It’s easy to construct thestd::vectorfrom a C style array using the two iterator constructor ofstd::vector, so there’s no real inconvenience in having both types.