Problem using struct inside my class

Started by
0 comments, last by JohnBolton 18 years, 7 months ago
I'm making a small game with Directx and I'm having problem to use my struct inside my class. I have this custom one : struct CUSTOMVERTEX { FLOAT x, y, z; DWORD color; }; In my class header i define it like that CUSTOMVERTEX g_Vertices; The problem is that when i use it inside a subfunction i do this CUSTOMVERTEX g_Vertices[]= { {-100.0f, 0.0f, 0.0f, 0.0f, 1.0f }, {-100.0f, 200.0f, 0.0f, 0.0f, 0.0f }, {100.0f, 0.0f, 0.0f, 1.0f, 0.0f }, {100.0f, 200.0f, 0.0f, 1.0f, 1.0f }, }; I don't need to indicate the size of this struct. But i can't do it when it's inside my class since i can only add the value like that at the declaration of the struct. Normaly i should do g_Vertices[0].x = 1 g_vertices[0].y = 2 etc etc. Is there any faster way to set my value inside my struct without adding the value one by one and telling all the time which case of the struct i want to add it? If i'm not clear enought tell me.
Advertisement
1. "CUSTOMVERTEX g_Vertices;" defines a single CUSTOMVERTEX, while "CUSTOMVERTEX g_Vertices[] = {...}" defines an array of CUSTOMVERTEX. Totally different. If you don't explicitly set the size of the array, it is automatically set to the number of elements you initialize it with. In your example, your array has 4 elements.

2. If you define g_Vertices in the class and then also define g_Vertices in a function, those are two totally different variables, even though they have the same name.

3. No, in general, there is no faster way of initializing a non-POD class. However, there may be some tricks or optimizations, but if initialization is not the bottleneck, then there isn't really a need to make it faster.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!

This topic is closed to new replies.

Advertisement