开发者

How is @private implemented?

开发者 https://www.devze.com 2023-01-29 00:35 出处:网络
In Objective-C, I\'m curious how access controls for instance variables, like @private,@protected, etc. are 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消