除了可访问性之外,访问说明符还有什么作用?
除了对派生类可见或不可见的正常解释之外,它们还有其他区别吗?
如果你让它更明显,它会占用更多或更少的内存,它会减慢速度还是......?
Besides the normal explenation of being visible or not to derived classes, is their any other difference?
If you make it more visible, is it taking more or less memory, does it slow thing down or...?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除了外部成员或派生类成员的可访问性之外,访问说明符还可能影响对象布局。
引用我的其他答案:
通常,内存地址对于数据成员,按照它们在类中定义的顺序增加。但是在遇到访问说明符(
private
、protected
、public
)的任何地方,此顺序都可能会被打乱。 C++ 对象模型内部对此进行了详细讨论>李普曼。摘自C/C++ 用户日志,
很有趣,不是吗?
Apart from the accessibility of members outside or to the derived classes, access specifiers might affect the object layout.
Quoting from my other answer:
Usually, memory address for data members increases in the order they're defined in the class . But this order may be disrupted at any place where the access-specifiers (
private
,protected
,public
) are encountered. This has been discussed in great detail in Inside the C++ Object Model by Lippman.An excerpt from C/C++ Users Journal,
Interesting, isn't it?
来自
n3225
,9.2 [class.mem]注释15这意味着给出以下声明:
标准仅强制执行以下断言:
@Nawaz 的引用可以解释为给出了可以自由混合的 4 个块,但事实并非如此。
Foo
的声明完全等同于:事实上,编译器完全忽略(为此目的)说明符是否出现一次或多次,并且每次指定它都是虚假的,最多会减慢编译速度,因为额外的解析。对于人类读者来说,这可能更清楚……但这非常主观。
From
n3225
, 9.2 [class.mem] note 15This means that given the following declaration:
Only the following assertions are enforced by the standard:
@Nawaz's citation can be interpreted as giving 4 blocks that can be freely intermixed, but this is not the case. The declaration of
Foo
is perfectly equivalent to:Indeed the compiler completely ignores (for this purpose) whether a specifier appeared once or multiple times, and specifying it each time is spurious and at best slows the compilation down because of the extra parsing. For a human reader, it might be clearer... but this is highly subjective.