views:

22

answers:

1

Please see comment:

static void drawAnObject() {
    Form *form = [[Form alloc] init];
    int i;

    [form randomizeCube];
    glColor3f(0.0, 0.0, 0.0);

    for(i = 0; i < MAX_CUBE; i++) {
        glutWireCube(form->cube[i]->size); 
            //compiler issues hard warning because form->cube is protected!!
    }
}

I would rather use the accessor I placed on the "Form" class so I could rather write something like:

glutWireCube([[form cube][i] size]);

But I get a "cannot convert to a pointer type" error when I attempt to compile this.

This is my accessor in the "Form" class:

@implementation Form
- (Cube *) cube {
    return *cube;
}

The variable "cube" is defined in the "Form" class' header file as follows:

@interface Form : NSObject <NSCopying> {
    Cube *cube[MAX_CUBE];
}

The following are the "Cube" class' variables as defined in its header file:

@interface Cube : NSObject <NSCopying> {
    double size;
    double positionX;
    double positionY;
    double positionZ;
}

...and the corresponding accessors in the implementation (.m) file:

- (double) size {
    return size;
}

- (void) setSize: (double) newSize {
    size = newSize;
}

- (double) positionX {
    return positionX;
}

- (void) setPositionX: (double) newPositionX {
    positionX = newPositionX;
}

- (double) positionY {
    return positionY;
}

- (void) setPositionY: (double) newPositionY {
    positionY = newPositionY;
}

- (double) positionZ {
    return positionZ;
}

- (void) setPositionZ: (double) newPositionZ {
    positionZ = newPositionZ;
}

I would like to avoid using an NSMutableArray o NSArray as I plan to port this part of the project's code to non-Cocoa platforms later on.

I've spent the last couple of hours searching for the correct way to do this. Is there "a right way" to do this using C-Style arrays and Objective-C accessors?

A: 

You're missing an asterisk in your accessor method. It is tricky because you cannot return array types from methods in C or Objective-C. Try this:

@implementation Form
- (Cube **) cube {
    return &cube[0]; // alternatively just "return cube;" will suffice
}
@end

This returns a pointer to a pointer, so when you subscript it with [form cube][i] you are still left with a pointer type, and in Objective-C receivers must be pointer types. This should allow you to do

glutWireCube([[form cube][i] size]);
dreamlax
Thank you very much for your prompt answer. I will try this and post my findings.
Rafael
Solved and understood. Thanks.
Rafael