Question

In Objective-C, I'm curious how access controls for instance variables, like @private,@protected, etc. are implemented.

I had considered that separate structures were being generated in some way like this:

@interface Foo {
  int bar;
  @private
  int baz;
  @public
  int qux;
}

=> something along the lines of

struct Class_Foo_Protected {
  int bar;
};

struct Class_Foo_Private {
  int baz;
};

struct Class_Foo_Public {
  int qux;
};

But I really have no idea. Anyone know how this was really done?

Was it helpful?

Solution

Those modifiers don’t change anything about the memory layout of your class. The compiler itself remembers which ivar is public, protected or private and emits errors if you try to access them from somewhere inappropriate. This is all done before any code is generated and doesn’t affect the generated code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top