Obj C object level arrays question

Posts: 48
Joined: 2009.05
Post: #1
Is there a way to declare an array in the header, so it's an object data member and local to the whole object, and yet not populate it till some initialize method in the .m body?

I know that you could do it like this:


GLfloat squareVertices[3];


squareVertices[0] = 1.0f;
squareVertices[1] = 1.0f;
squareVertices[2] = 1.0f;

but is there any way you know of to do it SOMETHING LIKE this: (because obviously this doesn't work... what would?)


GLfloat squareVertices[3];


squareVertices = {1.0f , 1.0f, 1.0f};
Quote this message in a reply
Posts: 1,563
Joined: 2003.10
Post: #2
About the best you'll be able to do is to write a convenience function.
#include <stdarg.h>

void populateGLfloatArray(GLfloat * array, unsigned int count, ...) {
  va_list args;
  unsigned int argIndex;
  va_start(args, count);
  for (argIndex = 0; argIndex < count; argIndex++) {
    array[argIndex] = va_arg(args, GLfloat);


populateGLfloatArray(squareVertices, 3, 1.0f, 1.0f, 1.0f);
Quote this message in a reply
Posts: 1,487
Joined: 2002.09
Post: #3
Why not just define it in a single file and put an extern declaration in your header?

extern GLfloat thing[3];

GLfloat thing[3] = {1.0f, 2.0f, 3.0f};

If you can't do that because you are using not static values to initialize it, fill it in the class's + initialize method.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Posts: 48
Joined: 2009.05
Post: #4
Thank you both for replying and your ideas.

Skorche - I really like your idea, but it's something I've never done before - using external files etc..

Could you please make it a little clearer or point me to an example that you know of?

Thank you both very much.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  question regarding releasing an object Gillissie 1 2,569 Mar 16, 2009 02:10 AM
Last Post: maximile
  Quick question on using arrays with CGFloats duncster 5 6,132 Dec 30, 2008 09:59 AM
Last Post: AnotherJake