除了可访问性之外,访问说明符还有什么作用?

发布于 2024-10-16 20:49:53 字数 83 浏览 3 评论 0原文

除了对派生类可见或不可见的正常解释之外,它们还有其他区别吗?

如果你让它更明显,它会占用更多或更少的内存,它会减慢速度还是......?

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 技术交流群。

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

发布评论

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

评论(2

酒中人 2024-10-23 20:49:53

除了外部成员或派生类成员的可访问性之外,访问说明符还可能影响对象布局。

引用我的其他答案

通常,内存地址对于数据成员,按照它们在类中定义的顺序增加。但是在遇到访问说明符(privateprotectedpublic)的任何地方,此顺序都可能会被打乱C++ 对象模型内部对此进行了详细讨论>李普曼

摘自C/C++ 用户日志

编译器不允许执行此操作
不过,重新排列本身。这
标准要求所有数据
相同 public:、protected: 或
私人:必须在其中列出
由编译器排序。 如果你
将您的数据与访问权分开
不过,编译器是
允许重新排列
访问说明符分隔的块
数据来改进布局
这是
为什么有些人喜欢设置访问权限
每个数据前面的说明符
会员

很有趣,不是吗?

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,

The compiler isn't allowed to do this
rearrangement itself, though. The
standard requires that all data that's
in the same public:, protected:, or
private: must be laid out in that
order by the compiler. If you
intersperse your data with access
specifiers, though, the compiler is
allowed to rearrange the
access-specifier-delimited blocks of
data to improve the layout
, which is
why some people like putting an access
specifier in front of every data
member
.

Interesting, isn't it?

还不是爱你 2024-10-23 20:49:53

来自n32259.2 [class.mem]注释15

分配具有相同访问控制(第 11 条)的(非联合)类的非静态数据成员,以便后面的成员在类对象中具有更高的地址。具有不同访问控制的非静态数据成员的分配顺序未指定(11)。

这意味着给出以下声明:

class Foo {
public:  int a;
private: int b;
public:  int c;
private: int d;
};

标准仅强制执行以下断言:

Foo foo;

assert(&foo.a < &foo.c);
assert(&foo.b < &foo.d);

@Nawaz 的引用可以解释为给出了可以自由混合的 4 个块,但事实并非如此。 Foo 的声明完全等同于:

class Foo' {
public:  int a,c;
private: int b,d;
};

事实上,编译器完全忽略(为此目的)说明符是否出现一次或多次,并且每次指定它都是虚假的,最多会减慢编译速度,因为额外的解析。对于人类读者来说,这可能更清楚……但这非常主观。

From n3225, 9.2 [class.mem] note 15

Nonstatic data members of a (non-union) class with the same access control (Clause 11) are allocated so that later members have higher addresses within a class object. The order of allocation of non-static data members with different access control is unspecified (11).

This means that given the following declaration:

class Foo {
public:  int a;
private: int b;
public:  int c;
private: int d;
};

Only the following assertions are enforced by the standard:

Foo foo;

assert(&foo.a < &foo.c);
assert(&foo.b < &foo.d);

@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:

class Foo' {
public:  int a,c;
private: int b,d;
};

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.

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