method argument wildcard
I'd like to do something like this:
I use three different "resisterVertices" methods right now, but I'd like to have one method that can be used by all three vertex types.
If I was using NS objects I presume I could use id as the argument type, and then recast them in the switch statement, but this method sits in that area between the Objective-C and C parts of my project.
Is this possible, without wrapping them in some sort of NS object? I'm happy (even keen) to make it a C function if that helps.
Code:
Vector2f *verticesA;
Vector3f *verticesB;
TexturedVertex *verticesC;
verticesA = malloc(10*sizeof(Vector2f));
verticesB = ...
verticesC = ...
[self registerVertices:verticesA ofNumber:10 forType:0];
[self registerVertices:verticesB ofNumber:22 forType:1];
[self registerVertices:verticesC ofNumber:39 forType:2];
--//--
Vector2f *vertices_BatchA;
Vector3f *vertices_BatchB;
TexturedVertex *vertices_BatchC;
- (void)registerVertices:([b]_wildcard_[/b]*)vertices ofNumber:(uint) forType:(uint)type
{
switch(type) {
case 0:
for(int i=0; i<number; i++)
vertices_BatchA[i] = vertices[i];
break;
case 1:
...
case 2:
...
}
}I use three different "resisterVertices" methods right now, but I'd like to have one method that can be used by all three vertex types.
If I was using NS objects I presume I could use id as the argument type, and then recast them in the switch statement, but this method sits in that area between the Objective-C and C parts of my project.
Is this possible, without wrapping them in some sort of NS object? I'm happy (even keen) to make it a C function if that helps.
Maybe you want to use void * ?
You can use a C++ template function to accomplish this. (http://www.cplusplus.com/doc/tutorial/templates/) You can compile C++ code with ObjectiveC by making that file's extension .mm instead of .m.
void* did the trick. I had tried it at one point, but didn't entirely understand how to use that pointer to access an array. This was the solution, with the void* argument:
Thanks for the help.
Code:
for(int i=0; i<max; i++)
something = *((Vector2f*)vertices + i)Thanks for the help.
Code:
something = ((Vector2f*)vertices)[i]should also work by the way.

