C++: how use arrays on our class's?

how i get the array size?
heres my class:
1
2
3
4
5
6
7
8
9
10
struct Vertex    //Overloaded Vertex Structure
{
	Vertex() {}
	Vertex(float x, float y, float z,
		float cr, float cg, float cb, float ca)
		: pos(x, y, z), color(cr, cg, cb, ca) {}

	XMFLOAT3 pos;
	XMFLOAT4 color;
};

heres how i try get the variable and structure sizes:
1
2
3
4
Triangle(Vertex PosColor[], HWND DxWindow, string ShaderFile, ID3D11Device *device, ID3D11DeviceContext *context)
		{
                      string strValues = to_string(sizeof(PosColor)) + "  " + to_string(sizeof(Vertex));
			MessageBox(NULL, strValues.c_str(), "array size", MB_OK);

the result is:
"4 28"
don't make sence to me... how the structure size is more big than is object?

PS: for create a topic, using code, i must 1st create it without a code and then edit it for add the code.
Last edited on
sizeof(PosColor) will return the size of the pointer. This is because when you pass an array into a function, it will decay into a pointer. I would recommend you to use std::vector<> or std::array<> instead of raw arrays. This generally is better practice as it doesn't require you to watch out for pointers, deallocation, and you can resize an std::vector.
To determine which one to choose:
-When the number of elements is fixed, you can use std::array
-When you have a variable number of elements, use std::vector
Again, I can't stress it enough: Always try to use std::array or std::vector instead of raw arrays, it will save you alot of frustration
Last edited on
thank you so much for all... yes i did the new change and works like a charm... thank you so much....
and i don't need convert the code from an array to vector.... but for get the void*, we must use the data() member. and the size() member give me the number of elements on vector.
thank you so much for all
Glad I could help :)
Registered users can post here. Sign in or register to post.