I’m a newer in Objective C. I found can access the private variable outside. I just get a warning Like follows:
@interface foo : NSObject
{
@private
int b;
}
-(id) init;
@end
//omit the implement
int main()
{
foo *a = [[foo alloc] init];
printf("%d", a->b);
}
So dose the private keyword only work on its subclass? If Yes, why need protected keyword
The code you provided accesses
ab, notb. Furthermore, you’re using the class namefoo, but you appear to be trying to access an instance variable namedab. If you want to access an instance variable, you’ll first need to create an instance of the class:The
@privatekeyword prevents you from accessingbfrom anywhere other than the methods defined by classfoo.Update:
If you’re compiling with an Objective-C compiler, you normally can’t:
If you’re able to get the code to compile and access
b, there’s something odd going on. What compiler are you using, and what settings?