Why does this work when I do that but not when I do this?

Started by
2 comments, last by j-locke 13 years ago
I'm trying to set an array and it works when I do this:

DWORD indices[3] = { 0, 1, 2 };

But it won't work if the array is defined in a class like this:




class Face
{
public:
DWORD indices[3];
};

void foo()
{
std::vector<Face> faces(12);
faces[0].indices = {0, 1, 2};
}



I simply want to know why that won't work?
Advertisement
It's a C++ technicality. The first case is the construction of an array, which supports that nice compact syntax. The second case is assignment to an array, which doesn't.

You can do it like this: std::vector<Face> faces(12);
Face f = {{0,1,2}};//construction
faces[0] = f;//assignment
Or like this: std::vector<Face> faces(12);
DWORD data = {0,1,2};//construction
std::copy(data, data+3, faces[0].indices);//assignment

It's a C++ technicality. The first case is the construction of an array, which supports that nice compact syntax. The second case is assignment to an array, which doesn't.

You can do it like this: std::vector<Face> faces(12);
Face f = {{0,1,2}};//construction
faces[0] = f;//assignment
Or like this: std::vector<Face> faces(12);
DWORD data = {0,1,2};//construction
std::copy(data, data+3, faces[0].indices);//assignment



Oh, interesting. So there is no way to do it in a single line? I may as well just set each component.
If you're looking for simplicity on the setting side, you could add a function (possibly even constructor) to the Face class to allow for setting the indices. Then it would give you a one liner on the side of utilizing the class.




faces[0].SetIndices(0,1,2);

This topic is closed to new replies.

Advertisement