• Advertisement
Sign in to follow this  

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

This topic is 2507 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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
Edited by Hodgman

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement