How to declare an array of floats as a class variable in Objective-C when the dimension is undefined at the class instantiation time?

前端 未结 3 998
执念已碎
执念已碎 2021-02-11 04:36

In Java, it would look like this:

class Foo
{
  float[] array;
}

Foo instance = new Foo();
instance.array = new float[10];
3条回答
  •  情深已故
    2021-02-11 05:06

    You can just use a pointer:

    float *array;
    // Allocate 10 floats -- always remember to multiple by the object size
    // when calling malloc
    array = (float *)malloc(10 * sizeof(float));
    ...
    // Deallocate array -- don't forget to do this when you're done with your object
    free(array);
    

    If you're using Objective-C++, you could instead do:

    float *array;
    array = new float[10];
    ...
    delete [] array;
    

提交回复
热议问题