How is @private implemented?
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 开发者_StackOverflowbar;
};
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?
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.
精彩评论