Archived

This topic is now archived and is closed to further replies.

Neoteric

c++ arrays and constructors

Recommended Posts

Neoteric    122
Hello, I''m a c++ noob and I''m having trouble passing arrays to constructors or methods, I want to pass the whole array in, what should I be doing instead? e.g. UINT g_Texture[MAX_TEXTURES] = {0}; Model f_Model(&g_Texture[0]); Model::Model(UINT *g_Texture) { g_LoadObj.ImportObj(&g_3DModel, FILE_NAME); g_LoadObj.AddMaterial(&g_3DModel, "cloth", "cloth.bmp", 255, 255, 255); ... Thanks.

Share this post


Link to post
Share on other sites
GoofProg    127
Model::Model(UINT g_Texture[])
{...
}

This will copy the array to the new g_Texture array
If you want to make it faster and you wanted to manipulate the original array, then just pass the address of the array





[edited by - GoofProg on July 5, 2003 5:44:03 PM]

Share this post


Link to post
Share on other sites
BrianL    530
This is going to be a quick and dirty explanation written between compiles, so be sure to check for favorite c/c++ book on how pointers and arrays work.

One common way of passing an array is to pass a pointer to it, and an integer which stores the number of elements in the array. A pointer to an array is treated much like a pointer to a single element. For a single element, you assign the pointer by taking the address of the element:

UINT Texture;
UINT* pTexture = &Texture;

In the case of an array, you need to take the address of the first element, and store elsewhere the range of the pointer.

UINT Texture;
UINT* pTexture = &Texture[0];

From your example:

UINT g_Texture[MAX_TEXTURES];

...
Model.Model(&g_Texture[0], MAX_TEXTURES);
...

Model::Model(UINT* g_Texture, int nTextures)
...

Share this post


Link to post
Share on other sites
chiuyan    122
GoofProg wrote "
Model::Model(UINT g_Texture[])
{...
}

This will copy the array to the new g_Texture array
If you want to make it faster and you wanted to manipulate the original array, then just pass the address of the array
"

huh?

what will copy what and why?

Share this post


Link to post
Share on other sites
Neoteric    122
Thanks BrianL and Goofprog,

This might seem to not be the problem then, I was passing in the pointer to the first element as the objects aleady knew the size (bad practice I know but it's only temporary), my syntax must be tangled further down the line as it only deals with that first element I pass in.

EDIT: The debugger is apparently giving the error "Expression cannot be evaluated," but the address is still valid there are no errors when building.

[edited by - Neoteric on July 5, 2003 6:50:51 PM]

Share this post


Link to post
Share on other sites