Correct syntax for accessing an array of c-style objects via objective-c messaging?

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 an accessory that I placed in the "Form" class, so I could write something like:

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

But when I try to compile this, I get the error "cannot convert to pointer type".

This is my accessor in the class "Form":

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

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

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

The following are class variables for the Cube class defined in its header file:

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

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

- (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 NSMutableArray o NSArray, as I plan to port this part of the project code to platforms without Cocoa later.

, . " " , C-Style Objective-C?

+3
1

. , C Objective-C. :

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

, , [form cube][i], - , Objective-C .

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

Source: https://habr.com/ru/post/1751165/


All Articles