@private是如何实现的?

发布于 2024-10-05 19:53:49 字数 411 浏览 0 评论 0原文

在 Objective-C 中,我很好奇如何实现实例变量的访问控制,例如 @private、@protected 等。

我曾考虑过以某种方式生成单独的结构,如下所示:

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

=> 类似于

struct Class_Foo_Protected {
  int bar;
};

struct Class_Foo_Private {
  int baz;
};

struct Class_Foo_Public {
  int qux;
};

但我真的不知道。有人知道这是怎么做到的吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

意犹 2024-10-12 19:53:49

这些修饰符不会改变类的内存布局。编译器本身会记住哪个 ivar 是公共的、受保护的或私有的,并且如果您尝试从不适当的地方访问它们,则会发出错误。这一切都是在生成任何代码之前完成的,不会影响生成的代码。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文