Sign in to follow this  
SteveDeFacto

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

Recommended Posts

I'm trying to set an array and it works when I do this:

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

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


[code]

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

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


I simply want to know why that won't work?

Share this post


Link to post
Share on other sites
It's a C++ technicality. The first case is the [b]construction [/b]of an array, which supports that nice compact syntax. The second case is [b]assignment [/b]to an array, which doesn't.

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

Share this post


Link to post
Share on other sites
[quote name='Hodgman' timestamp='1302487840' post='4796908']
It's a C++ technicality. The first case is the [b]construction [/b]of an array, which supports that nice compact syntax. The second case is [b]assignment [/b]to an array, which doesn't.

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this