1

i have helper C functions in some Objective C classes. Just found out that the values of global, static C variables which i use in these functions are shared between instances of the class (duh), which is not what i want.

Is there a way to declare these variables local to instances of the class, so that they are visible by the helper functions without passing them explicitly?

1
  • 1
    I'm sorry - but I don't understand the question. It sounds like you want a global variable that is only visible to the object? Which doesn't make sense. Commented May 27, 2011 at 21:40

2 Answers 2

2

Is there a way to declare these variables local to instances of the class

Sure, make them instance variables.

But:

so that they are visible by the helper functions without passing them explicitly?

You can pass the object into the function. If you have appropriate accessors, the function can get them. And if you have mutators, it can modify them, too.

But if you're doing that, you might as well just create a method, and automatically have access to the instance variables.

Sign up to request clarification or add additional context in comments.

1 Comment

ah yes, like this static inline void timeCriticalTask(void *refToSelf){}? i'm using these for DSP stuff and want to avoid method calls where necessary.. thanks!
2

want to avoid method calls where necessary

logically separate it so your low level code is in c or c++, then add the required data to your objc class:

/* c example */

typedef struct t_generator {
    UInt32 a;
} t_generator;


static void Generate(t_generator* const gen) {
    /.../   
}

@interface MONObjCGeneratorContainer : NSObject
{
    t_generator generator;
    NSString * name;
    UInt32 b;
}
@end

if the data interface is as simple you can just access them from the instance:

- (void)method { GenerateB(&b); }

that should meet all the requirements you have posted (so far).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.